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 package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path"
"github.com/coreos/coreos-cloudinit/datasource" "github.com/coreos/coreos-cloudinit/datasource"
"github.com/coreos/coreos-cloudinit/initialize" "github.com/coreos/coreos-cloudinit/initialize"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/system" "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) env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName)
if len(userdataBytes) > 0 { if len(userdataBytes) > 0 {
if err := processUserdata(string(userdataBytes), env); err != nil { 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 { if !ignoreFailure {
os.Exit(1) os.Exit(1)
} }
} }
} else { } else {
fmt.Println("No user data to handle.") fmt.Println("No user-data to handle.")
} }
if convertNetconf != "" { if len(metadataBytes) > 0 {
if err := processNetconf(convertNetconf, configdrive); err != nil { if err := processMetadata(string(metadataBytes), env); err != nil {
fmt.Printf("Failed to process network config: %v\n", err) fmt.Printf("Failed to process meta-data: %v\n", err)
if !ignoreFailure { if !ignoreFailure {
os.Exit(1) 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 return err
} }
func processNetconf(convertNetconf, configdrive string) error { func processMetadata(metadata string, env *initialize.Environment) error {
openstackRoot := path.Join(configdrive, "openstack") parsed, err := initialize.ParseMetaData(metadata)
metadataFilename := path.Join(openstackRoot, "latest", "meta_data.json")
metadataBytes, err := ioutil.ReadFile(metadataFilename)
if err != nil { if err != nil {
fmt.Printf("Failed parsing meta-data: %v\n", err)
return err return err
} }
err = initialize.PrepWorkspace(env.Workspace())
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))
if err != nil { if err != nil {
fmt.Printf("Failed preparing workspace: %v\n", err)
return err return err
} }
var interfaces []network.InterfaceGenerator return initialize.Apply(parsed, env)
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)
} }

View File

@ -3,10 +3,13 @@ package initialize
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"path"
"github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml" "github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/system" "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) 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) commands := make(map[string]string, 0)
reload := false reload := false
for _, unit := range cfg.Coreos.Units { for _, unit := range cfg.Coreos.Units {