mirror of
https://github.com/rancher/os.git
synced 2025-09-08 10:11:46 +00:00
Update vendor
This commit is contained in:
294
vendor/github.com/coreos/coreos-cloudinit/initialize/config.go
generated
vendored
294
vendor/github.com/coreos/coreos-cloudinit/initialize/config.go
generated
vendored
@@ -1,294 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"path"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"github.com/coreos/coreos-cloudinit/network"
|
||||
"github.com/coreos/coreos-cloudinit/system"
|
||||
)
|
||||
|
||||
// CloudConfigFile represents a CoreOS specific configuration option that can generate
|
||||
// an associated system.File to be written to disk
|
||||
type CloudConfigFile interface {
|
||||
// File should either return (*system.File, error), or (nil, nil) if nothing
|
||||
// needs to be done for this configuration option.
|
||||
File() (*system.File, error)
|
||||
}
|
||||
|
||||
// CloudConfigUnit represents a CoreOS specific configuration option that can generate
|
||||
// associated system.Units to be created/enabled appropriately
|
||||
type CloudConfigUnit interface {
|
||||
Units() []system.Unit
|
||||
}
|
||||
|
||||
// Apply renders a CloudConfig to an Environment. This can involve things like
|
||||
// configuring the hostname, adding new users, writing various configuration
|
||||
// files to disk, and manipulating systemd services.
|
||||
func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Environment) error {
|
||||
if cfg.Hostname != "" {
|
||||
if err := system.SetHostname(cfg.Hostname); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Set hostname to %s", cfg.Hostname)
|
||||
}
|
||||
|
||||
for _, user := range cfg.Users {
|
||||
if user.Name == "" {
|
||||
log.Printf("User object has no 'name' field, skipping")
|
||||
continue
|
||||
}
|
||||
|
||||
if system.UserExists(&user) {
|
||||
log.Printf("User '%s' exists, ignoring creation-time fields", user.Name)
|
||||
if user.PasswordHash != "" {
|
||||
log.Printf("Setting '%s' user's password", user.Name)
|
||||
if err := system.SetUserPassword(user.Name, user.PasswordHash); err != nil {
|
||||
log.Printf("Failed setting '%s' user's password: %v", user.Name, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Creating user '%s'", user.Name)
|
||||
if err := system.CreateUser(&user); err != nil {
|
||||
log.Printf("Failed creating user '%s': %v", user.Name, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(user.SSHAuthorizedKeys) > 0 {
|
||||
log.Printf("Authorizing %d SSH keys for user '%s'", len(user.SSHAuthorizedKeys), user.Name)
|
||||
if err := system.AuthorizeSSHKeys(user.Name, env.SSHKeyName(), user.SSHAuthorizedKeys); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.SSHImportGithubUser != "" {
|
||||
log.Printf("Authorizing github user %s SSH keys for CoreOS user '%s'", user.SSHImportGithubUser, user.Name)
|
||||
if err := SSHImportGithubUser(user.Name, user.SSHImportGithubUser); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, u := range user.SSHImportGithubUsers {
|
||||
log.Printf("Authorizing github user %s SSH keys for CoreOS user '%s'", u, user.Name)
|
||||
if err := SSHImportGithubUser(user.Name, u); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.SSHImportURL != "" {
|
||||
log.Printf("Authorizing SSH keys for CoreOS user '%s' from '%s'", user.Name, user.SSHImportURL)
|
||||
if err := SSHImportKeysFromURL(user.Name, user.SSHImportURL); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(cfg.SSHAuthorizedKeys) > 0 {
|
||||
err := system.AuthorizeSSHKeys("core", env.SSHKeyName(), cfg.SSHAuthorizedKeys)
|
||||
if err == nil {
|
||||
log.Printf("Authorized SSH keys for core user")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var writeFiles []system.File
|
||||
for _, file := range cfg.WriteFiles {
|
||||
writeFiles = append(writeFiles, system.File{File: file})
|
||||
}
|
||||
|
||||
for _, ccf := range []CloudConfigFile{
|
||||
system.OEM{OEM: cfg.CoreOS.OEM},
|
||||
system.Update{Update: cfg.CoreOS.Update, ReadConfig: system.DefaultReadConfig},
|
||||
system.EtcHosts{EtcHosts: cfg.ManageEtcHosts},
|
||||
system.Flannel{Flannel: cfg.CoreOS.Flannel},
|
||||
} {
|
||||
f, err := ccf.File()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f != nil {
|
||||
writeFiles = append(writeFiles, *f)
|
||||
}
|
||||
}
|
||||
|
||||
var units []system.Unit
|
||||
for _, u := range cfg.CoreOS.Units {
|
||||
units = append(units, system.Unit{Unit: u})
|
||||
}
|
||||
|
||||
for _, ccu := range []CloudConfigUnit{
|
||||
system.Etcd{Etcd: cfg.CoreOS.Etcd},
|
||||
system.Etcd2{Etcd2: cfg.CoreOS.Etcd2},
|
||||
system.Fleet{Fleet: cfg.CoreOS.Fleet},
|
||||
system.Locksmith{Locksmith: cfg.CoreOS.Locksmith},
|
||||
system.Update{Update: cfg.CoreOS.Update, ReadConfig: system.DefaultReadConfig},
|
||||
} {
|
||||
units = append(units, ccu.Units()...)
|
||||
}
|
||||
|
||||
wroteEnvironment := false
|
||||
for _, file := range writeFiles {
|
||||
fullPath, err := system.WriteFile(&file, env.Root())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if path.Clean(file.Path) == "/etc/environment" {
|
||||
wroteEnvironment = true
|
||||
}
|
||||
log.Printf("Wrote file %s to filesystem", fullPath)
|
||||
}
|
||||
|
||||
if !wroteEnvironment {
|
||||
ef := env.DefaultEnvironmentFile()
|
||||
if ef != nil {
|
||||
err := system.WriteEnvFile(ef, env.Root())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Updated /etc/environment")
|
||||
}
|
||||
}
|
||||
|
||||
if len(ifaces) > 0 {
|
||||
units = append(units, createNetworkingUnits(ifaces)...)
|
||||
if err := system.RestartNetwork(ifaces); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
um := system.NewUnitManager(env.Root())
|
||||
return processUnits(units, env.Root(), um)
|
||||
}
|
||||
|
||||
func createNetworkingUnits(interfaces []network.InterfaceGenerator) (units []system.Unit) {
|
||||
appendNewUnit := func(units []system.Unit, name, content string) []system.Unit {
|
||||
if content == "" {
|
||||
return units
|
||||
}
|
||||
return append(units, system.Unit{Unit: config.Unit{
|
||||
Name: name,
|
||||
Runtime: true,
|
||||
Content: content,
|
||||
}})
|
||||
}
|
||||
for _, i := range interfaces {
|
||||
units = appendNewUnit(units, fmt.Sprintf("%s.netdev", i.Filename()), i.Netdev())
|
||||
units = appendNewUnit(units, fmt.Sprintf("%s.link", i.Filename()), i.Link())
|
||||
units = appendNewUnit(units, fmt.Sprintf("%s.network", i.Filename()), i.Network())
|
||||
}
|
||||
return units
|
||||
}
|
||||
|
||||
// processUnits takes a set of Units and applies them to the given root using
|
||||
// the given UnitManager. This can involve things like writing unit files to
|
||||
// disk, masking/unmasking units, or invoking systemd
|
||||
// commands against units. It returns any error encountered.
|
||||
func processUnits(units []system.Unit, root string, um system.UnitManager) error {
|
||||
type action struct {
|
||||
unit system.Unit
|
||||
command string
|
||||
}
|
||||
actions := make([]action, 0, len(units))
|
||||
reload := false
|
||||
restartNetworkd := false
|
||||
for _, unit := range units {
|
||||
if unit.Name == "" {
|
||||
log.Printf("Skipping unit without name")
|
||||
continue
|
||||
}
|
||||
|
||||
if unit.Content != "" {
|
||||
log.Printf("Writing unit %q to filesystem", unit.Name)
|
||||
if err := um.PlaceUnit(unit); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Wrote unit %q", unit.Name)
|
||||
reload = true
|
||||
}
|
||||
|
||||
for _, dropin := range unit.DropIns {
|
||||
if dropin.Name != "" && dropin.Content != "" {
|
||||
log.Printf("Writing drop-in unit %q to filesystem", dropin.Name)
|
||||
if err := um.PlaceUnitDropIn(unit, dropin); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Wrote drop-in unit %q", dropin.Name)
|
||||
reload = true
|
||||
}
|
||||
}
|
||||
|
||||
if unit.Mask {
|
||||
log.Printf("Masking unit file %q", unit.Name)
|
||||
if err := um.MaskUnit(unit); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if unit.Runtime {
|
||||
log.Printf("Ensuring runtime unit file %q is unmasked", unit.Name)
|
||||
if err := um.UnmaskUnit(unit); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if unit.Enable {
|
||||
if unit.Group() != "network" {
|
||||
log.Printf("Enabling unit file %q", unit.Name)
|
||||
if err := um.EnableUnitFile(unit); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Enabled unit %q", unit.Name)
|
||||
} else {
|
||||
log.Printf("Skipping enable for network-like unit %q", unit.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if unit.Group() == "network" {
|
||||
restartNetworkd = true
|
||||
} else if unit.Command != "" {
|
||||
actions = append(actions, action{unit, unit.Command})
|
||||
}
|
||||
}
|
||||
|
||||
if reload {
|
||||
if err := um.DaemonReload(); err != nil {
|
||||
return errors.New(fmt.Sprintf("failed systemd daemon-reload: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
if restartNetworkd {
|
||||
log.Printf("Restarting systemd-networkd")
|
||||
networkd := system.Unit{Unit: config.Unit{Name: "systemd-networkd.service"}}
|
||||
res, err := um.RunUnitCommand(networkd, "restart")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Restarted systemd-networkd (%s)", res)
|
||||
}
|
||||
|
||||
for _, action := range actions {
|
||||
log.Printf("Calling unit command %q on %q'", action.command, action.unit.Name)
|
||||
res, err := um.RunUnitCommand(action.unit, action.command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Result of %q on %q: %s", action.command, action.unit.Name, res)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
63
vendor/github.com/coreos/coreos-cloudinit/network/debian.go
generated
vendored
63
vendor/github.com/coreos/coreos-cloudinit/network/debian.go
generated
vendored
@@ -1,63 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ProcessDebianNetconf(config []byte) ([]InterfaceGenerator, error) {
|
||||
log.Println("Processing Debian network config")
|
||||
lines := formatConfig(string(config))
|
||||
stanzas, err := parseStanzas(lines)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
interfaces := make([]*stanzaInterface, 0, len(stanzas))
|
||||
for _, stanza := range stanzas {
|
||||
switch s := stanza.(type) {
|
||||
case *stanzaInterface:
|
||||
interfaces = append(interfaces, s)
|
||||
}
|
||||
}
|
||||
log.Printf("Parsed %d network interfaces\n", len(interfaces))
|
||||
|
||||
log.Println("Processed Debian network config")
|
||||
return buildInterfaces(interfaces), nil
|
||||
}
|
||||
|
||||
func formatConfig(config string) []string {
|
||||
lines := []string{}
|
||||
config = strings.Replace(config, "\\\n", "", -1)
|
||||
for config != "" {
|
||||
split := strings.SplitN(config, "\n", 2)
|
||||
line := strings.TrimSpace(split[0])
|
||||
|
||||
if len(split) == 2 {
|
||||
config = split[1]
|
||||
} else {
|
||||
config = ""
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "#") || line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
lines = append(lines, line)
|
||||
}
|
||||
return lines
|
||||
}
|
169
vendor/github.com/coreos/coreos-cloudinit/network/digitalocean.go
generated
vendored
169
vendor/github.com/coreos/coreos-cloudinit/network/digitalocean.go
generated
vendored
@@ -1,169 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean"
|
||||
)
|
||||
|
||||
func ProcessDigitalOceanNetconf(config digitalocean.Metadata) ([]InterfaceGenerator, error) {
|
||||
log.Println("Processing DigitalOcean network config")
|
||||
|
||||
log.Println("Parsing nameservers")
|
||||
nameservers, err := parseNameservers(config.DNS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("Parsed %d nameservers\n", len(nameservers))
|
||||
|
||||
log.Println("Parsing interfaces")
|
||||
generators, err := parseInterfaces(config.Interfaces, nameservers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("Parsed %d network interfaces\n", len(generators))
|
||||
|
||||
log.Println("Processed DigitalOcean network config")
|
||||
return generators, nil
|
||||
}
|
||||
|
||||
func parseNameservers(config digitalocean.DNS) ([]net.IP, error) {
|
||||
nameservers := make([]net.IP, 0, len(config.Nameservers))
|
||||
for _, ns := range config.Nameservers {
|
||||
if ip := net.ParseIP(ns); ip == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as nameserver IP address", ns)
|
||||
} else {
|
||||
nameservers = append(nameservers, ip)
|
||||
}
|
||||
}
|
||||
return nameservers, nil
|
||||
}
|
||||
|
||||
func parseInterfaces(config digitalocean.Interfaces, nameservers []net.IP) ([]InterfaceGenerator, error) {
|
||||
generators := make([]InterfaceGenerator, 0, len(config.Public)+len(config.Private))
|
||||
for _, iface := range config.Public {
|
||||
if generator, err := parseInterface(iface, nameservers, true); err == nil {
|
||||
generators = append(generators, &physicalInterface{*generator})
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, iface := range config.Private {
|
||||
if generator, err := parseInterface(iface, []net.IP{}, false); err == nil {
|
||||
generators = append(generators, &physicalInterface{*generator})
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return generators, nil
|
||||
}
|
||||
|
||||
func parseInterface(iface digitalocean.Interface, nameservers []net.IP, useRoute bool) (*logicalInterface, error) {
|
||||
routes := make([]route, 0)
|
||||
addresses := make([]net.IPNet, 0)
|
||||
if iface.IPv4 != nil {
|
||||
var ip, mask, gateway net.IP
|
||||
if ip = net.ParseIP(iface.IPv4.IPAddress); ip == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as IPv4 address", iface.IPv4.IPAddress)
|
||||
}
|
||||
if mask = net.ParseIP(iface.IPv4.Netmask); mask == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as IPv4 mask", iface.IPv4.Netmask)
|
||||
}
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.IPMask(mask),
|
||||
})
|
||||
|
||||
if useRoute {
|
||||
if gateway = net.ParseIP(iface.IPv4.Gateway); gateway == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as IPv4 gateway", iface.IPv4.Gateway)
|
||||
}
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.IPMask(net.IPv4zero),
|
||||
},
|
||||
gateway: gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
if iface.IPv6 != nil {
|
||||
var ip, gateway net.IP
|
||||
if ip = net.ParseIP(iface.IPv6.IPAddress); ip == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as IPv6 address", iface.IPv6.IPAddress)
|
||||
}
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.CIDRMask(iface.IPv6.Cidr, net.IPv6len*8),
|
||||
})
|
||||
|
||||
if useRoute {
|
||||
if gateway = net.ParseIP(iface.IPv6.Gateway); gateway == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as IPv6 gateway", iface.IPv6.Gateway)
|
||||
}
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.IPMask(net.IPv6zero),
|
||||
},
|
||||
gateway: gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
if iface.AnchorIPv4 != nil {
|
||||
var ip, mask net.IP
|
||||
if ip = net.ParseIP(iface.AnchorIPv4.IPAddress); ip == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as anchor IPv4 address", iface.AnchorIPv4.IPAddress)
|
||||
}
|
||||
if mask = net.ParseIP(iface.AnchorIPv4.Netmask); mask == nil {
|
||||
return nil, fmt.Errorf("could not parse %q as anchor IPv4 mask", iface.AnchorIPv4.Netmask)
|
||||
}
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.IPMask(mask),
|
||||
})
|
||||
|
||||
if useRoute {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.IPMask(net.IPv4zero),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
hwaddr, err := net.ParseMAC(iface.MAC)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if nameservers == nil {
|
||||
nameservers = []net.IP{}
|
||||
}
|
||||
|
||||
return &logicalInterface{
|
||||
hwaddr: hwaddr,
|
||||
config: configMethodStatic{
|
||||
addresses: addresses,
|
||||
nameservers: nameservers,
|
||||
routes: routes,
|
||||
},
|
||||
}, nil
|
||||
}
|
340
vendor/github.com/coreos/coreos-cloudinit/network/interface.go
generated
vendored
340
vendor/github.com/coreos/coreos-cloudinit/network/interface.go
generated
vendored
@@ -1,340 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InterfaceGenerator interface {
|
||||
Name() string
|
||||
Filename() string
|
||||
Netdev() string
|
||||
Link() string
|
||||
Network() string
|
||||
Type() string
|
||||
ModprobeParams() string
|
||||
}
|
||||
|
||||
type networkInterface interface {
|
||||
InterfaceGenerator
|
||||
Children() []networkInterface
|
||||
setConfigDepth(int)
|
||||
}
|
||||
|
||||
type logicalInterface struct {
|
||||
name string
|
||||
hwaddr net.HardwareAddr
|
||||
config configMethod
|
||||
children []networkInterface
|
||||
configDepth int
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Name() string {
|
||||
return i.name
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Network() string {
|
||||
config := fmt.Sprintln("[Match]")
|
||||
if i.name != "" {
|
||||
config += fmt.Sprintf("Name=%s\n", i.name)
|
||||
}
|
||||
if i.hwaddr != nil {
|
||||
config += fmt.Sprintf("MACAddress=%s\n", i.hwaddr)
|
||||
}
|
||||
config += "\n[Network]\n"
|
||||
|
||||
for _, child := range i.children {
|
||||
switch iface := child.(type) {
|
||||
case *vlanInterface:
|
||||
config += fmt.Sprintf("VLAN=%s\n", iface.name)
|
||||
case *bondInterface:
|
||||
config += fmt.Sprintf("Bond=%s\n", iface.name)
|
||||
}
|
||||
}
|
||||
|
||||
switch conf := i.config.(type) {
|
||||
case configMethodStatic:
|
||||
for _, nameserver := range conf.nameservers {
|
||||
config += fmt.Sprintf("DNS=%s\n", nameserver)
|
||||
}
|
||||
for _, addr := range conf.addresses {
|
||||
config += fmt.Sprintf("\n[Address]\nAddress=%s\n", addr.String())
|
||||
}
|
||||
for _, route := range conf.routes {
|
||||
config += fmt.Sprintf("\n[Route]\nDestination=%s\nGateway=%s\n", route.destination.String(), route.gateway)
|
||||
}
|
||||
case configMethodDHCP:
|
||||
config += "DHCP=true\n"
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Link() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Netdev() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Filename() string {
|
||||
name := i.name
|
||||
if name == "" {
|
||||
name = i.hwaddr.String()
|
||||
}
|
||||
return fmt.Sprintf("%02x-%s", i.configDepth, name)
|
||||
}
|
||||
|
||||
func (i *logicalInterface) Children() []networkInterface {
|
||||
return i.children
|
||||
}
|
||||
|
||||
func (i *logicalInterface) ModprobeParams() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (i *logicalInterface) setConfigDepth(depth int) {
|
||||
i.configDepth = depth
|
||||
}
|
||||
|
||||
type physicalInterface struct {
|
||||
logicalInterface
|
||||
}
|
||||
|
||||
func (p *physicalInterface) Type() string {
|
||||
return "physical"
|
||||
}
|
||||
|
||||
type bondInterface struct {
|
||||
logicalInterface
|
||||
slaves []string
|
||||
options map[string]string
|
||||
}
|
||||
|
||||
func (b *bondInterface) Netdev() string {
|
||||
config := fmt.Sprintf("[NetDev]\nKind=bond\nName=%s\n", b.name)
|
||||
if b.hwaddr != nil {
|
||||
config += fmt.Sprintf("MACAddress=%s\n", b.hwaddr.String())
|
||||
}
|
||||
|
||||
config += fmt.Sprintf("\n[Bond]\n")
|
||||
for _, name := range sortedKeys(b.options) {
|
||||
config += fmt.Sprintf("%s=%s\n", name, b.options[name])
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (b *bondInterface) Type() string {
|
||||
return "bond"
|
||||
}
|
||||
|
||||
func (b *bondInterface) ModprobeParams() string {
|
||||
params := ""
|
||||
for _, name := range sortedKeys(b.options) {
|
||||
params += fmt.Sprintf("%s=%s ", name, b.options[name])
|
||||
}
|
||||
params = strings.TrimSuffix(params, " ")
|
||||
return params
|
||||
}
|
||||
|
||||
type vlanInterface struct {
|
||||
logicalInterface
|
||||
id int
|
||||
rawDevice string
|
||||
}
|
||||
|
||||
func (v *vlanInterface) Netdev() string {
|
||||
config := fmt.Sprintf("[NetDev]\nKind=vlan\nName=%s\n", v.name)
|
||||
switch c := v.config.(type) {
|
||||
case configMethodStatic:
|
||||
if c.hwaddress != nil {
|
||||
config += fmt.Sprintf("MACAddress=%s\n", c.hwaddress)
|
||||
}
|
||||
case configMethodDHCP:
|
||||
if c.hwaddress != nil {
|
||||
config += fmt.Sprintf("MACAddress=%s\n", c.hwaddress)
|
||||
}
|
||||
}
|
||||
config += fmt.Sprintf("\n[VLAN]\nId=%d\n", v.id)
|
||||
return config
|
||||
}
|
||||
|
||||
func (v *vlanInterface) Type() string {
|
||||
return "vlan"
|
||||
}
|
||||
|
||||
func buildInterfaces(stanzas []*stanzaInterface) []InterfaceGenerator {
|
||||
interfaceMap := createInterfaces(stanzas)
|
||||
linkAncestors(interfaceMap)
|
||||
markConfigDepths(interfaceMap)
|
||||
|
||||
interfaces := make([]InterfaceGenerator, 0, len(interfaceMap))
|
||||
for _, name := range sortedInterfaces(interfaceMap) {
|
||||
interfaces = append(interfaces, interfaceMap[name])
|
||||
}
|
||||
|
||||
return interfaces
|
||||
}
|
||||
|
||||
func createInterfaces(stanzas []*stanzaInterface) map[string]networkInterface {
|
||||
interfaceMap := make(map[string]networkInterface)
|
||||
for _, iface := range stanzas {
|
||||
switch iface.kind {
|
||||
case interfaceBond:
|
||||
bondOptions := make(map[string]string)
|
||||
for _, k := range []string{"mode", "miimon", "lacp-rate"} {
|
||||
if v, ok := iface.options["bond-"+k]; ok && len(v) > 0 {
|
||||
bondOptions[k] = v[0]
|
||||
}
|
||||
}
|
||||
interfaceMap[iface.name] = &bondInterface{
|
||||
logicalInterface{
|
||||
name: iface.name,
|
||||
config: iface.configMethod,
|
||||
children: []networkInterface{},
|
||||
},
|
||||
iface.options["bond-slaves"],
|
||||
bondOptions,
|
||||
}
|
||||
for _, slave := range iface.options["bond-slaves"] {
|
||||
if _, ok := interfaceMap[slave]; !ok {
|
||||
interfaceMap[slave] = &physicalInterface{
|
||||
logicalInterface{
|
||||
name: slave,
|
||||
config: configMethodManual{},
|
||||
children: []networkInterface{},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case interfacePhysical:
|
||||
if _, ok := iface.configMethod.(configMethodLoopback); ok {
|
||||
continue
|
||||
}
|
||||
interfaceMap[iface.name] = &physicalInterface{
|
||||
logicalInterface{
|
||||
name: iface.name,
|
||||
config: iface.configMethod,
|
||||
children: []networkInterface{},
|
||||
},
|
||||
}
|
||||
|
||||
case interfaceVLAN:
|
||||
var rawDevice string
|
||||
id, _ := strconv.Atoi(iface.options["id"][0])
|
||||
if device := iface.options["raw_device"]; len(device) == 1 {
|
||||
rawDevice = device[0]
|
||||
if _, ok := interfaceMap[rawDevice]; !ok {
|
||||
interfaceMap[rawDevice] = &physicalInterface{
|
||||
logicalInterface{
|
||||
name: rawDevice,
|
||||
config: configMethodManual{},
|
||||
children: []networkInterface{},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
interfaceMap[iface.name] = &vlanInterface{
|
||||
logicalInterface{
|
||||
name: iface.name,
|
||||
config: iface.configMethod,
|
||||
children: []networkInterface{},
|
||||
},
|
||||
id,
|
||||
rawDevice,
|
||||
}
|
||||
}
|
||||
}
|
||||
return interfaceMap
|
||||
}
|
||||
|
||||
func linkAncestors(interfaceMap map[string]networkInterface) {
|
||||
for _, name := range sortedInterfaces(interfaceMap) {
|
||||
iface := interfaceMap[name]
|
||||
switch i := iface.(type) {
|
||||
case *vlanInterface:
|
||||
if parent, ok := interfaceMap[i.rawDevice]; ok {
|
||||
switch p := parent.(type) {
|
||||
case *physicalInterface:
|
||||
p.children = append(p.children, iface)
|
||||
case *bondInterface:
|
||||
p.children = append(p.children, iface)
|
||||
}
|
||||
}
|
||||
case *bondInterface:
|
||||
for _, slave := range i.slaves {
|
||||
if parent, ok := interfaceMap[slave]; ok {
|
||||
switch p := parent.(type) {
|
||||
case *physicalInterface:
|
||||
p.children = append(p.children, iface)
|
||||
case *bondInterface:
|
||||
p.children = append(p.children, iface)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func markConfigDepths(interfaceMap map[string]networkInterface) {
|
||||
rootInterfaceMap := make(map[string]networkInterface)
|
||||
for k, v := range interfaceMap {
|
||||
rootInterfaceMap[k] = v
|
||||
}
|
||||
|
||||
for _, iface := range interfaceMap {
|
||||
for _, child := range iface.Children() {
|
||||
delete(rootInterfaceMap, child.Name())
|
||||
}
|
||||
}
|
||||
for _, iface := range rootInterfaceMap {
|
||||
setDepth(iface)
|
||||
}
|
||||
}
|
||||
|
||||
func setDepth(iface networkInterface) int {
|
||||
maxDepth := 0
|
||||
for _, child := range iface.Children() {
|
||||
if depth := setDepth(child); depth > maxDepth {
|
||||
maxDepth = depth
|
||||
}
|
||||
}
|
||||
iface.setConfigDepth(maxDepth)
|
||||
return (maxDepth + 1)
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]string) (keys []string) {
|
||||
for key := range m {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return
|
||||
}
|
||||
|
||||
func sortedInterfaces(m map[string]networkInterface) (keys []string) {
|
||||
for key := range m {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return
|
||||
}
|
115
vendor/github.com/coreos/coreos-cloudinit/network/packet.go
generated
vendored
115
vendor/github.com/coreos/coreos-cloudinit/network/packet.go
generated
vendored
@@ -1,115 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/datasource/metadata/packet"
|
||||
)
|
||||
|
||||
func ProcessPacketNetconf(netdata packet.NetworkData) ([]InterfaceGenerator, error) {
|
||||
var nameservers []net.IP
|
||||
if netdata.DNS != nil {
|
||||
nameservers = netdata.DNS
|
||||
} else {
|
||||
nameservers = append(nameservers, net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4"))
|
||||
}
|
||||
|
||||
generators, err := parseNetwork(netdata, nameservers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return generators, nil
|
||||
}
|
||||
|
||||
func parseNetwork(netdata packet.NetworkData, nameservers []net.IP) ([]InterfaceGenerator, error) {
|
||||
var interfaces []InterfaceGenerator
|
||||
var addresses []net.IPNet
|
||||
var routes []route
|
||||
for _, netblock := range netdata.Netblocks {
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: netblock.Address,
|
||||
Mask: net.IPMask(netblock.Netmask),
|
||||
})
|
||||
if netblock.Public == false {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4(10, 0, 0, 0),
|
||||
Mask: net.IPv4Mask(255, 0, 0, 0),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
} else {
|
||||
if netblock.AddressFamily == 4 {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.IPMask(net.IPv4zero),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
} else {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.IPMask(net.IPv6zero),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bond := bondInterface{
|
||||
logicalInterface: logicalInterface{
|
||||
name: "bond0",
|
||||
config: configMethodStatic{
|
||||
addresses: addresses,
|
||||
nameservers: nameservers,
|
||||
routes: routes,
|
||||
},
|
||||
},
|
||||
options: map[string]string{
|
||||
"Mode": "802.3ad",
|
||||
"LACPTransmitRate": "fast",
|
||||
"MIIMonitorSec": ".2",
|
||||
"UpDelaySec": ".2",
|
||||
"DownDelaySec": ".2",
|
||||
},
|
||||
}
|
||||
|
||||
bond.hwaddr, _ = net.ParseMAC(netdata.Interfaces[0].Mac)
|
||||
|
||||
for index, iface := range netdata.Interfaces {
|
||||
bond.slaves = append(bond.slaves, iface.Name)
|
||||
|
||||
interfaces = append(interfaces, &physicalInterface{
|
||||
logicalInterface: logicalInterface{
|
||||
name: iface.Name,
|
||||
config: configMethodStatic{
|
||||
nameservers: nameservers,
|
||||
},
|
||||
children: []networkInterface{&bond},
|
||||
configDepth: index,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
interfaces = append(interfaces, &bond)
|
||||
|
||||
return interfaces, nil
|
||||
}
|
340
vendor/github.com/coreos/coreos-cloudinit/network/stanza.go
generated
vendored
340
vendor/github.com/coreos/coreos-cloudinit/network/stanza.go
generated
vendored
@@ -1,340 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type stanza interface{}
|
||||
|
||||
type stanzaAuto struct {
|
||||
interfaces []string
|
||||
}
|
||||
|
||||
type stanzaInterface struct {
|
||||
name string
|
||||
kind interfaceKind
|
||||
auto bool
|
||||
configMethod configMethod
|
||||
options map[string][]string
|
||||
}
|
||||
|
||||
type interfaceKind int
|
||||
|
||||
const (
|
||||
interfaceBond = interfaceKind(iota)
|
||||
interfacePhysical
|
||||
interfaceVLAN
|
||||
)
|
||||
|
||||
type route struct {
|
||||
destination net.IPNet
|
||||
gateway net.IP
|
||||
}
|
||||
|
||||
type configMethod interface{}
|
||||
|
||||
type configMethodStatic struct {
|
||||
addresses []net.IPNet
|
||||
nameservers []net.IP
|
||||
routes []route
|
||||
hwaddress net.HardwareAddr
|
||||
}
|
||||
|
||||
type configMethodLoopback struct{}
|
||||
|
||||
type configMethodManual struct{}
|
||||
|
||||
type configMethodDHCP struct {
|
||||
hwaddress net.HardwareAddr
|
||||
}
|
||||
|
||||
func parseStanzas(lines []string) (stanzas []stanza, err error) {
|
||||
rawStanzas, err := splitStanzas(lines)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stanzas = make([]stanza, 0, len(rawStanzas))
|
||||
for _, rawStanza := range rawStanzas {
|
||||
if stanza, err := parseStanza(rawStanza); err == nil {
|
||||
stanzas = append(stanzas, stanza)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
autos := make([]string, 0)
|
||||
interfaceMap := make(map[string]*stanzaInterface)
|
||||
for _, stanza := range stanzas {
|
||||
switch c := stanza.(type) {
|
||||
case *stanzaAuto:
|
||||
autos = append(autos, c.interfaces...)
|
||||
case *stanzaInterface:
|
||||
interfaceMap[c.name] = c
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the auto attribute
|
||||
for _, auto := range autos {
|
||||
if iface, ok := interfaceMap[auto]; ok {
|
||||
iface.auto = true
|
||||
}
|
||||
}
|
||||
|
||||
return stanzas, nil
|
||||
}
|
||||
|
||||
func splitStanzas(lines []string) ([][]string, error) {
|
||||
var curStanza []string
|
||||
stanzas := make([][]string, 0)
|
||||
for _, line := range lines {
|
||||
if isStanzaStart(line) {
|
||||
if curStanza != nil {
|
||||
stanzas = append(stanzas, curStanza)
|
||||
}
|
||||
curStanza = []string{line}
|
||||
} else if curStanza != nil {
|
||||
curStanza = append(curStanza, line)
|
||||
} else {
|
||||
return nil, fmt.Errorf("missing stanza start %q", line)
|
||||
}
|
||||
}
|
||||
|
||||
if curStanza != nil {
|
||||
stanzas = append(stanzas, curStanza)
|
||||
}
|
||||
|
||||
return stanzas, nil
|
||||
}
|
||||
|
||||
func isStanzaStart(line string) bool {
|
||||
switch strings.Split(line, " ")[0] {
|
||||
case "auto":
|
||||
fallthrough
|
||||
case "iface":
|
||||
fallthrough
|
||||
case "mapping":
|
||||
return true
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "allow-") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func parseStanza(rawStanza []string) (stanza, error) {
|
||||
if len(rawStanza) == 0 {
|
||||
panic("empty stanza")
|
||||
}
|
||||
tokens := strings.Fields(rawStanza[0])
|
||||
if len(tokens) < 2 {
|
||||
return nil, fmt.Errorf("malformed stanza start %q", rawStanza[0])
|
||||
}
|
||||
|
||||
kind := tokens[0]
|
||||
attributes := tokens[1:]
|
||||
|
||||
switch kind {
|
||||
case "auto":
|
||||
return parseAutoStanza(attributes, rawStanza[1:])
|
||||
case "iface":
|
||||
return parseInterfaceStanza(attributes, rawStanza[1:])
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown stanza %q", kind)
|
||||
}
|
||||
}
|
||||
|
||||
func parseAutoStanza(attributes []string, options []string) (*stanzaAuto, error) {
|
||||
return &stanzaAuto{interfaces: attributes}, nil
|
||||
}
|
||||
|
||||
func parseInterfaceStanza(attributes []string, options []string) (*stanzaInterface, error) {
|
||||
if len(attributes) != 3 {
|
||||
return nil, fmt.Errorf("incorrect number of attributes")
|
||||
}
|
||||
|
||||
iface := attributes[0]
|
||||
confMethod := attributes[2]
|
||||
|
||||
optionMap := make(map[string][]string, 0)
|
||||
for _, option := range options {
|
||||
if strings.HasPrefix(option, "post-up") {
|
||||
tokens := strings.SplitAfterN(option, " ", 2)
|
||||
if len(tokens) != 2 {
|
||||
continue
|
||||
}
|
||||
if v, ok := optionMap["post-up"]; ok {
|
||||
optionMap["post-up"] = append(v, tokens[1])
|
||||
} else {
|
||||
optionMap["post-up"] = []string{tokens[1]}
|
||||
}
|
||||
} else if strings.HasPrefix(option, "pre-down") {
|
||||
tokens := strings.SplitAfterN(option, " ", 2)
|
||||
if len(tokens) != 2 {
|
||||
continue
|
||||
}
|
||||
if v, ok := optionMap["pre-down"]; ok {
|
||||
optionMap["pre-down"] = append(v, tokens[1])
|
||||
} else {
|
||||
optionMap["pre-down"] = []string{tokens[1]}
|
||||
}
|
||||
} else {
|
||||
tokens := strings.Fields(option)
|
||||
optionMap[tokens[0]] = tokens[1:]
|
||||
}
|
||||
}
|
||||
|
||||
var conf configMethod
|
||||
switch confMethod {
|
||||
case "static":
|
||||
config := configMethodStatic{
|
||||
addresses: make([]net.IPNet, 1),
|
||||
routes: make([]route, 0),
|
||||
nameservers: make([]net.IP, 0),
|
||||
}
|
||||
if addresses, ok := optionMap["address"]; ok {
|
||||
if len(addresses) == 1 {
|
||||
config.addresses[0].IP = net.ParseIP(addresses[0])
|
||||
}
|
||||
}
|
||||
if netmasks, ok := optionMap["netmask"]; ok {
|
||||
if len(netmasks) == 1 {
|
||||
config.addresses[0].Mask = net.IPMask(net.ParseIP(netmasks[0]).To4())
|
||||
}
|
||||
}
|
||||
if config.addresses[0].IP == nil || config.addresses[0].Mask == nil {
|
||||
return nil, fmt.Errorf("malformed static network config for %q", iface)
|
||||
}
|
||||
if gateways, ok := optionMap["gateway"]; ok {
|
||||
if len(gateways) == 1 {
|
||||
config.routes = append(config.routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4(0, 0, 0, 0),
|
||||
Mask: net.IPv4Mask(0, 0, 0, 0),
|
||||
},
|
||||
gateway: net.ParseIP(gateways[0]),
|
||||
})
|
||||
}
|
||||
}
|
||||
if hwaddress, err := parseHwaddress(optionMap, iface); err == nil {
|
||||
config.hwaddress = hwaddress
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
for _, nameserver := range optionMap["dns-nameservers"] {
|
||||
config.nameservers = append(config.nameservers, net.ParseIP(nameserver))
|
||||
}
|
||||
for _, postup := range optionMap["post-up"] {
|
||||
if strings.HasPrefix(postup, "route add") {
|
||||
route := route{}
|
||||
fields := strings.Fields(postup)
|
||||
for i, field := range fields[:len(fields)-1] {
|
||||
switch field {
|
||||
case "-net":
|
||||
if _, dst, err := net.ParseCIDR(fields[i+1]); err == nil {
|
||||
route.destination = *dst
|
||||
} else {
|
||||
route.destination.IP = net.ParseIP(fields[i+1])
|
||||
}
|
||||
case "netmask":
|
||||
route.destination.Mask = net.IPMask(net.ParseIP(fields[i+1]).To4())
|
||||
case "gw":
|
||||
route.gateway = net.ParseIP(fields[i+1])
|
||||
}
|
||||
}
|
||||
if route.destination.IP != nil && route.destination.Mask != nil && route.gateway != nil {
|
||||
config.routes = append(config.routes, route)
|
||||
}
|
||||
}
|
||||
}
|
||||
conf = config
|
||||
case "loopback":
|
||||
conf = configMethodLoopback{}
|
||||
case "manual":
|
||||
conf = configMethodManual{}
|
||||
case "dhcp":
|
||||
config := configMethodDHCP{}
|
||||
if hwaddress, err := parseHwaddress(optionMap, iface); err == nil {
|
||||
config.hwaddress = hwaddress
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
conf = config
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid config method %q", confMethod)
|
||||
}
|
||||
|
||||
if _, ok := optionMap["vlan_raw_device"]; ok {
|
||||
return parseVLANStanza(iface, conf, attributes, optionMap)
|
||||
}
|
||||
|
||||
if strings.Contains(iface, ".") {
|
||||
return parseVLANStanza(iface, conf, attributes, optionMap)
|
||||
}
|
||||
|
||||
if _, ok := optionMap["bond-slaves"]; ok {
|
||||
return parseBondStanza(iface, conf, attributes, optionMap)
|
||||
}
|
||||
|
||||
return parsePhysicalStanza(iface, conf, attributes, optionMap)
|
||||
}
|
||||
|
||||
func parseHwaddress(options map[string][]string, iface string) (net.HardwareAddr, error) {
|
||||
if hwaddress, ok := options["hwaddress"]; ok && len(hwaddress) == 2 {
|
||||
switch hwaddress[0] {
|
||||
case "ether":
|
||||
if address, err := net.ParseMAC(hwaddress[1]); err == nil {
|
||||
return address, nil
|
||||
}
|
||||
return nil, fmt.Errorf("malformed hwaddress option for %q", iface)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func parseBondStanza(iface string, conf configMethod, attributes []string, options map[string][]string) (*stanzaInterface, error) {
|
||||
return &stanzaInterface{name: iface, kind: interfaceBond, configMethod: conf, options: options}, nil
|
||||
}
|
||||
|
||||
func parsePhysicalStanza(iface string, conf configMethod, attributes []string, options map[string][]string) (*stanzaInterface, error) {
|
||||
return &stanzaInterface{name: iface, kind: interfacePhysical, configMethod: conf, options: options}, nil
|
||||
}
|
||||
|
||||
func parseVLANStanza(iface string, conf configMethod, attributes []string, options map[string][]string) (*stanzaInterface, error) {
|
||||
var id string
|
||||
if strings.Contains(iface, ".") {
|
||||
tokens := strings.Split(iface, ".")
|
||||
id = tokens[len(tokens)-1]
|
||||
} else if strings.HasPrefix(iface, "vlan") {
|
||||
id = strings.TrimPrefix(iface, "vlan")
|
||||
} else {
|
||||
return nil, fmt.Errorf("malformed vlan name %q", iface)
|
||||
}
|
||||
|
||||
if _, err := strconv.Atoi(id); err != nil {
|
||||
return nil, fmt.Errorf("malformed vlan name %q", iface)
|
||||
}
|
||||
options["id"] = []string{id}
|
||||
options["raw_device"] = options["vlan_raw_device"]
|
||||
|
||||
return &stanzaInterface{name: iface, kind: interfaceVLAN, configMethod: conf, options: options}, nil
|
||||
}
|
174
vendor/github.com/coreos/coreos-cloudinit/network/vmware.go
generated
vendored
174
vendor/github.com/coreos/coreos-cloudinit/network/vmware.go
generated
vendored
@@ -1,174 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func ProcessVMwareNetconf(config map[string]string) ([]InterfaceGenerator, error) {
|
||||
log.Println("Processing VMware network config")
|
||||
|
||||
log.Println("Parsing nameservers")
|
||||
var nameservers []net.IP
|
||||
for i := 0; ; i++ {
|
||||
if ipStr, ok := config[fmt.Sprintf("dns.server.%d", i)]; ok {
|
||||
if ip := net.ParseIP(ipStr); ip != nil {
|
||||
nameservers = append(nameservers, ip)
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid nameserver: %q", ipStr)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Printf("Parsed %d nameservers", len(nameservers))
|
||||
|
||||
var interfaces []InterfaceGenerator
|
||||
for i := 0; ; i++ {
|
||||
var addresses []net.IPNet
|
||||
var routes []route
|
||||
var err error
|
||||
var dhcp bool
|
||||
iface := &physicalInterface{}
|
||||
|
||||
log.Printf("Proccessing interface %d", i)
|
||||
|
||||
log.Println("Processing DHCP")
|
||||
if dhcp, err = processDHCPConfig(config, fmt.Sprintf("interface.%d.", i)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("Processing addresses")
|
||||
if as, err := processAddressConfig(config, fmt.Sprintf("interface.%d.", i)); err == nil {
|
||||
addresses = append(addresses, as...)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("Processing routes")
|
||||
if rs, err := processRouteConfig(config, fmt.Sprintf("interface.%d.", i)); err == nil {
|
||||
routes = append(routes, rs...)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if mac, ok := config[fmt.Sprintf("interface.%d.mac", i)]; ok {
|
||||
log.Printf("Parsing interface %d MAC address: %q", i, mac)
|
||||
if hwaddr, err := net.ParseMAC(mac); err == nil {
|
||||
iface.hwaddr = hwaddr
|
||||
} else {
|
||||
return nil, fmt.Errorf("error while parsing MAC address: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if name, ok := config[fmt.Sprintf("interface.%d.name", i)]; ok {
|
||||
log.Printf("Parsing interface %d name: %q", i, name)
|
||||
iface.name = name
|
||||
}
|
||||
|
||||
if len(addresses) > 0 || len(routes) > 0 {
|
||||
iface.config = configMethodStatic{
|
||||
hwaddress: iface.hwaddr,
|
||||
addresses: addresses,
|
||||
nameservers: nameservers,
|
||||
routes: routes,
|
||||
}
|
||||
} else if dhcp {
|
||||
iface.config = configMethodDHCP{
|
||||
hwaddress: iface.hwaddr,
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
interfaces = append(interfaces, iface)
|
||||
}
|
||||
|
||||
return interfaces, nil
|
||||
}
|
||||
|
||||
func processAddressConfig(config map[string]string, prefix string) (addresses []net.IPNet, err error) {
|
||||
for a := 0; ; a++ {
|
||||
prefix := fmt.Sprintf("%sip.%d.", prefix, a)
|
||||
|
||||
addressStr, ok := config[prefix+"address"]
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
ip, network, err := net.ParseCIDR(addressStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid address: %q", addressStr)
|
||||
}
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: ip,
|
||||
Mask: network.Mask,
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func processRouteConfig(config map[string]string, prefix string) (routes []route, err error) {
|
||||
for r := 0; ; r++ {
|
||||
prefix := fmt.Sprintf("%sroute.%d.", prefix, r)
|
||||
|
||||
gatewayStr, gok := config[prefix+"gateway"]
|
||||
destinationStr, dok := config[prefix+"destination"]
|
||||
if gok && !dok {
|
||||
return nil, fmt.Errorf("missing destination key")
|
||||
} else if !gok && dok {
|
||||
return nil, fmt.Errorf("missing gateway key")
|
||||
} else if !gok && !dok {
|
||||
break
|
||||
}
|
||||
|
||||
gateway := net.ParseIP(gatewayStr)
|
||||
if gateway == nil {
|
||||
return nil, fmt.Errorf("invalid gateway: %q", gatewayStr)
|
||||
}
|
||||
|
||||
_, destination, err := net.ParseCIDR(destinationStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routes = append(routes, route{
|
||||
destination: *destination,
|
||||
gateway: gateway,
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func processDHCPConfig(config map[string]string, prefix string) (dhcp bool, err error) {
|
||||
dhcpStr, ok := config[prefix+"dhcp"]
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
switch dhcpStr {
|
||||
case "yes":
|
||||
return true, nil
|
||||
case "no":
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("invalid DHCP option: %q", dhcpStr)
|
||||
}
|
||||
}
|
95
vendor/github.com/coreos/coreos-cloudinit/system/networkd.go
generated
vendored
95
vendor/github.com/coreos/coreos-cloudinit/system/networkd.go
generated
vendored
@@ -1,95 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"github.com/coreos/coreos-cloudinit/network"
|
||||
|
||||
"github.com/docker/libcontainer/netlink"
|
||||
)
|
||||
|
||||
func RestartNetwork(interfaces []network.InterfaceGenerator) (err error) {
|
||||
defer func() {
|
||||
if e := restartNetworkd(); e != nil {
|
||||
err = e
|
||||
}
|
||||
}()
|
||||
|
||||
if err = downNetworkInterfaces(interfaces); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = maybeProbe8012q(interfaces); err != nil {
|
||||
return
|
||||
}
|
||||
return maybeProbeBonding(interfaces)
|
||||
}
|
||||
|
||||
func downNetworkInterfaces(interfaces []network.InterfaceGenerator) error {
|
||||
sysInterfaceMap := make(map[string]*net.Interface)
|
||||
if systemInterfaces, err := net.Interfaces(); err == nil {
|
||||
for _, iface := range systemInterfaces {
|
||||
iface := iface
|
||||
sysInterfaceMap[iface.Name] = &iface
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, iface := range interfaces {
|
||||
if systemInterface, ok := sysInterfaceMap[iface.Name()]; ok {
|
||||
log.Printf("Taking down interface %q\n", systemInterface.Name)
|
||||
if err := netlink.NetworkLinkDown(systemInterface); err != nil {
|
||||
log.Printf("Error while downing interface %q (%s). Continuing...\n", systemInterface.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func maybeProbe8012q(interfaces []network.InterfaceGenerator) error {
|
||||
for _, iface := range interfaces {
|
||||
if iface.Type() == "vlan" {
|
||||
log.Printf("Probing LKM %q (%q)\n", "8021q", "8021q")
|
||||
return exec.Command("modprobe", "8021q").Run()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func maybeProbeBonding(interfaces []network.InterfaceGenerator) error {
|
||||
for _, iface := range interfaces {
|
||||
if iface.Type() == "bond" {
|
||||
args := append([]string{"bonding"}, strings.Split(iface.ModprobeParams(), " ")...)
|
||||
log.Printf("Probing LKM %q (%q)\n", "bonding", args)
|
||||
return exec.Command("modprobe", args...).Run()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restartNetworkd() error {
|
||||
log.Printf("Restarting networkd.service\n")
|
||||
networkd := Unit{config.Unit{Name: "systemd-networkd.service"}}
|
||||
_, err := NewUnitManager("").RunUnitCommand(networkd, "restart")
|
||||
return err
|
||||
}
|
205
vendor/github.com/coreos/coreos-cloudinit/system/systemd.go
generated
vendored
205
vendor/github.com/coreos/coreos-cloudinit/system/systemd.go
generated
vendored
@@ -1,205 +0,0 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-systemd/dbus"
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
func NewUnitManager(root string) UnitManager {
|
||||
return &systemd{root}
|
||||
}
|
||||
|
||||
type systemd struct {
|
||||
root string
|
||||
}
|
||||
|
||||
// fakeMachineID is placed on non-usr CoreOS images and should
|
||||
// never be used as a true MachineID
|
||||
const fakeMachineID = "42000000000000000000000000000042"
|
||||
|
||||
// PlaceUnit writes a unit file at its desired destination, creating parent
|
||||
// directories as necessary.
|
||||
func (s *systemd) PlaceUnit(u Unit) error {
|
||||
file := File{config.File{
|
||||
Path: u.Destination(s.root),
|
||||
Content: u.Content,
|
||||
RawFilePermissions: "0644",
|
||||
}}
|
||||
|
||||
_, err := WriteFile(&file, "/")
|
||||
return err
|
||||
}
|
||||
|
||||
// PlaceUnitDropIn writes a unit drop-in file at its desired destination,
|
||||
// creating parent directories as necessary.
|
||||
func (s *systemd) PlaceUnitDropIn(u Unit, d config.UnitDropIn) error {
|
||||
file := File{config.File{
|
||||
Path: u.DropInDestination(s.root, d),
|
||||
Content: d.Content,
|
||||
RawFilePermissions: "0644",
|
||||
}}
|
||||
|
||||
_, err := WriteFile(&file, "/")
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *systemd) EnableUnitFile(u Unit) error {
|
||||
conn, err := dbus.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
units := []string{u.Name}
|
||||
_, _, err = conn.EnableUnitFiles(units, u.Runtime, true)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *systemd) RunUnitCommand(u Unit, c string) (string, error) {
|
||||
conn, err := dbus.New()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var fn func(string, string) (string, error)
|
||||
switch c {
|
||||
case "start":
|
||||
fn = conn.StartUnit
|
||||
case "stop":
|
||||
fn = conn.StopUnit
|
||||
case "restart":
|
||||
fn = conn.RestartUnit
|
||||
case "reload":
|
||||
fn = conn.ReloadUnit
|
||||
case "try-restart":
|
||||
fn = conn.TryRestartUnit
|
||||
case "reload-or-restart":
|
||||
fn = conn.ReloadOrRestartUnit
|
||||
case "reload-or-try-restart":
|
||||
fn = conn.ReloadOrTryRestartUnit
|
||||
default:
|
||||
return "", fmt.Errorf("Unsupported systemd command %q", c)
|
||||
}
|
||||
|
||||
return fn(u.Name, "replace")
|
||||
}
|
||||
|
||||
func (s *systemd) DaemonReload() error {
|
||||
conn, err := dbus.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return conn.Reload()
|
||||
}
|
||||
|
||||
// MaskUnit masks the given Unit by symlinking its unit file to
|
||||
// /dev/null, analogous to `systemctl mask`.
|
||||
// N.B.: Unlike `systemctl mask`, this function will *remove any existing unit
|
||||
// file at the location*, to ensure that the mask will succeed.
|
||||
func (s *systemd) MaskUnit(u Unit) error {
|
||||
masked := u.Destination(s.root)
|
||||
if _, err := os.Stat(masked); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(path.Dir(masked), os.FileMode(0755)); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err := os.Remove(masked); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Symlink("/dev/null", masked)
|
||||
}
|
||||
|
||||
// UnmaskUnit is analogous to systemd's unit_file_unmask. If the file
|
||||
// associated with the given Unit is empty or appears to be a symlink to
|
||||
// /dev/null, it is removed.
|
||||
func (s *systemd) UnmaskUnit(u Unit) error {
|
||||
masked := u.Destination(s.root)
|
||||
ne, err := nullOrEmpty(masked)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ne {
|
||||
log.Printf("%s is not null or empty, refusing to unmask", masked)
|
||||
return nil
|
||||
}
|
||||
return os.Remove(masked)
|
||||
}
|
||||
|
||||
// nullOrEmpty checks whether a given path appears to be an empty regular file
|
||||
// or a symlink to /dev/null
|
||||
func nullOrEmpty(path string) (bool, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
m := fi.Mode()
|
||||
if m.IsRegular() && fi.Size() <= 0 {
|
||||
return true, nil
|
||||
}
|
||||
if m&os.ModeCharDevice > 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func ExecuteScript(scriptPath string) (string, error) {
|
||||
props := []dbus.Property{
|
||||
dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),
|
||||
dbus.PropExecStart([]string{"/bin/bash", scriptPath}, false),
|
||||
}
|
||||
|
||||
base := path.Base(scriptPath)
|
||||
name := fmt.Sprintf("coreos-cloudinit-%s.service", base)
|
||||
|
||||
log.Printf("Creating transient systemd unit '%s'", name)
|
||||
|
||||
conn, err := dbus.New()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = conn.StartTransientUnit(name, "replace", props...)
|
||||
return name, err
|
||||
}
|
||||
|
||||
func SetHostname(hostname string) error {
|
||||
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
|
||||
}
|
||||
|
||||
func Hostname() (string, error) {
|
||||
return os.Hostname()
|
||||
}
|
||||
|
||||
func MachineID(root string) string {
|
||||
contents, _ := ioutil.ReadFile(path.Join(root, "etc", "machine-id"))
|
||||
id := strings.TrimSpace(string(contents))
|
||||
|
||||
if id == fakeMachineID {
|
||||
id = ""
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
Reference in New Issue
Block a user