fix lock and resize order

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Василий Толстов 2017-11-22 11:51:25 +03:00
parent 99671182f1
commit d7b5d86bdb

View File

@ -42,41 +42,48 @@ type CloudConfigUnit interface {
Units() []system.Unit
}
func isLock(env *Environment) bool {
if _, err := os.Stat(path.Join(env.Workspace(), ".lock")); err != nil {
return false
}
return true
}
func Lock(env *Environment) error {
if !isLock(env) {
fp, err := os.OpenFile(path.Join(env.Workspace(), ".lock"), os.O_WRONLY|os.O_CREATE|os.O_EXCL|os.O_TRUNC, os.FileMode(0644))
if err != nil {
return err
}
return fp.Close()
}
return nil
}
// 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 {
var err error
if cfg.ResizeRootfs {
log.Printf("resize root filesystem")
if err = system.ResizeRootFS(); err != nil {
return err
}
}
for _, cmdline := range cfg.RunCMD {
prog := strings.Fields(cmdline)[0]
args := strings.Fields(cmdline)[1:]
exec.Command(prog, args...).Run()
}
lockf := path.Join(env.Workspace(), ".lock")
if _, err = os.Stat(lockf); err == nil {
return nil
}
if err = os.MkdirAll(env.Workspace(), os.FileMode(0755)); err != nil {
return err
}
if !isLock(env) {
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 == "" {
@ -84,6 +91,7 @@ func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Env
continue
}
if !isLock(env) {
if system.UserExists(&user) {
log.Printf("User '%s' exists, ignoring creation-time fields", user.Name)
if user.PasswordHash != "" {
@ -105,6 +113,7 @@ func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Env
log.Printf("Failed lock/unlock 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)
@ -141,6 +150,7 @@ func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Env
}
}
if !isLock(env) {
var writeFiles []system.File
for _, file := range cfg.WriteFiles {
writeFiles = append(writeFiles, system.File{File: file})
@ -210,14 +220,16 @@ func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Env
if err = processUnits(units, env.Root(), um); err != nil {
return err
}
}
fp, err := os.OpenFile(lockf, os.O_WRONLY|os.O_CREATE|os.O_EXCL|os.O_TRUNC, os.FileMode(0644))
if err != nil {
if cfg.ResizeRootfs {
log.Printf("resize root filesystem")
if err = system.ResizeRootFS(); err != nil {
return err
}
fp.Close()
}
return nil
return Lock(env)
}
func createNetworkingUnits(interfaces []network.InterfaceGenerator) (units []system.Unit) {