refactor(netconf): Move netconf processing and handle metadata

This commit is contained in:
Alex Crawford 2014-06-18 12:08:10 -07:00
parent 840c208b60
commit e6cf83a2e5
2 changed files with 53 additions and 45 deletions

View File

@ -1,16 +1,12 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/coreos/coreos-cloudinit/datasource"
"github.com/coreos/coreos-cloudinit/initialize"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/system"
)
@ -89,25 +85,38 @@ func main() {
}
}
fmt.Printf("Fetching meta-data from datasource of type %q\n", ds.Type())
metadataBytes, err := ds.FetchMetadata()
if err != nil {
fmt.Printf("Failed fetching meta-data from datasource: %v\n", err)
if ignoreFailure {
os.Exit(0)
} else {
os.Exit(1)
}
}
env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName)
if len(userdataBytes) > 0 {
if err := processUserdata(string(userdataBytes), env); err != nil {
fmt.Printf("Failed resolving user-data: %v\n", err)
fmt.Printf("Failed to process user-data: %v\n", err)
if !ignoreFailure {
os.Exit(1)
}
}
} else {
fmt.Println("No user data to handle.")
fmt.Println("No user-data to handle.")
}
if convertNetconf != "" {
if err := processNetconf(convertNetconf, configdrive); err != nil {
fmt.Printf("Failed to process network config: %v\n", err)
if len(metadataBytes) > 0 {
if err := processMetadata(string(metadataBytes), env); err != nil {
fmt.Printf("Failed to process meta-data: %v\n", err)
if !ignoreFailure {
os.Exit(1)
}
}
} else {
fmt.Println("No meta-data to handle.")
}
}
@ -142,47 +151,17 @@ func processUserdata(userdata string, env *initialize.Environment) error {
return err
}
func processNetconf(convertNetconf, configdrive string) error {
openstackRoot := path.Join(configdrive, "openstack")
metadataFilename := path.Join(openstackRoot, "latest", "meta_data.json")
metadataBytes, err := ioutil.ReadFile(metadataFilename)
func processMetadata(metadata string, env *initialize.Environment) error {
parsed, err := initialize.ParseMetaData(metadata)
if err != nil {
fmt.Printf("Failed parsing meta-data: %v\n", err)
return err
}
var metadata struct {
NetworkConfig struct {
ContentPath string `json:"content_path"`
} `json:"network_config"`
}
if err := json.Unmarshal(metadataBytes, &metadata); err != nil {
return err
}
configPath := metadata.NetworkConfig.ContentPath
if configPath == "" {
fmt.Printf("No network config specified in %q.\n", metadataFilename)
return nil
}
netconfBytes, err := ioutil.ReadFile(path.Join(openstackRoot, configPath))
err = initialize.PrepWorkspace(env.Workspace())
if err != nil {
fmt.Printf("Failed preparing workspace: %v\n", err)
return err
}
var interfaces []network.InterfaceGenerator
switch convertNetconf {
case "debian":
interfaces, err = network.ProcessDebianNetconf(string(netconfBytes))
default:
return fmt.Errorf("Unsupported network config format %q", convertNetconf)
}
if err != nil {
return err
}
if err := system.WriteNetworkdConfigs(interfaces); err != nil {
return err
}
return system.RestartNetwork(interfaces)
return initialize.Apply(parsed, env)
}

View File

@ -3,10 +3,13 @@ package initialize
import (
"errors"
"fmt"
"io/ioutil"
"log"
"path"
"github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/system"
)
@ -228,6 +231,32 @@ func Apply(cfg CloudConfig, env *Environment) error {
log.Printf("Wrote file %s to filesystem", path)
}
if env.NetconfType() != "" {
netconfBytes, err := ioutil.ReadFile(path.Join(env.ConfigRoot(), cfg.NetworkConfigPath))
if err != nil {
return err
}
var interfaces []network.InterfaceGenerator
switch env.NetconfType() {
case "debian":
interfaces, err = network.ProcessDebianNetconf(string(netconfBytes))
default:
return fmt.Errorf("Unsupported network config format %q", env.NetconfType())
}
if err != nil {
return err
}
if err := system.WriteNetworkdConfigs(interfaces); err != nil {
return err
}
if err := system.RestartNetwork(interfaces); err != nil {
return err
}
}
commands := make(map[string]string, 0)
reload := false
for _, unit := range cfg.Coreos.Units {