Add support for builds using rescue mode
This commit is contained in:
parent
e539133d8c
commit
064e6c7e08
|
@ -35,6 +35,8 @@ type Config struct {
|
||||||
UserDataFile string `mapstructure:"user_data_file"`
|
UserDataFile string `mapstructure:"user_data_file"`
|
||||||
SSHKeys []string `mapstructure:"ssh_keys"`
|
SSHKeys []string `mapstructure:"ssh_keys"`
|
||||||
|
|
||||||
|
RescueMode string `mapstructure:"rescue"`
|
||||||
|
|
||||||
ctx interpolate.Context
|
ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,11 +68,12 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
|
||||||
state.Put("server_id", serverCreateResult.Server.ID)
|
state.Put("server_id", serverCreateResult.Server.ID)
|
||||||
|
|
||||||
_, errCh := client.Action.WatchProgress(context.TODO(), serverCreateResult.Action)
|
_, errCh := client.Action.WatchProgress(context.TODO(), serverCreateResult.Action)
|
||||||
|
watch:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err1 := <-errCh:
|
case err1 := <-errCh:
|
||||||
if err1 == nil {
|
if err1 == nil {
|
||||||
return multistep.ActionContinue
|
break watch
|
||||||
} else {
|
} else {
|
||||||
err := fmt.Errorf("Error creating server: %s", err)
|
err := fmt.Errorf("Error creating server: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -82,6 +83,17 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.RescueMode != "" {
|
||||||
|
if err := setRescue(ctx, client, serverCreateResult.Server, c.RescueMode, sshKeys); err != nil {
|
||||||
|
err := fmt.Errorf("Error enabling rescue mode: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
|
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
|
||||||
|
@ -101,3 +113,48 @@ func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
|
||||||
"Error destroying server. Please destroy it manually: %s", err))
|
"Error destroying server. Please destroy it manually: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setRescue(ctx context.Context, client *hcloud.Client, server *hcloud.Server, rescue string, sshKeys []*hcloud.SSHKey) error {
|
||||||
|
rescueChanged := false
|
||||||
|
if server.RescueEnabled {
|
||||||
|
rescueChanged = true
|
||||||
|
action, _, err := client.Server.DisableRescue(ctx, server)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := waitForServerAction(ctx, client, action, server); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rescue != "" {
|
||||||
|
rescueChanged = true
|
||||||
|
res, _, err := client.Server.EnableRescue(ctx, server, hcloud.ServerEnableRescueOpts{
|
||||||
|
Type: hcloud.ServerRescueType(rescue),
|
||||||
|
SSHKeys: sshKeys,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := waitForServerAction(ctx, client, res.Action, server); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rescueChanged {
|
||||||
|
action, _, err := client.Server.Reset(ctx, server)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := waitForServerAction(ctx, client, action, server); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForServerAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action, server *hcloud.Server) error {
|
||||||
|
_, errCh := client.Action.WatchProgress(ctx, action)
|
||||||
|
if err := <-errCh; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -72,6 +72,8 @@ builder.
|
||||||
- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
|
- `ssh_keys` (array of strings) - List of SSH keys by name or id to be added
|
||||||
to image on launch.
|
to image on launch.
|
||||||
|
|
||||||
|
- `rescue` (string) - Enable and boot in to the specified rescue system. This enables simple installation of custom operating systems. `linux64` `linux32` or `freebsd64`
|
||||||
|
|
||||||
## Basic Example
|
## Basic Example
|
||||||
|
|
||||||
Here is a basic example. It is completely valid as soon as you enter your own
|
Here is a basic example. It is completely valid as soon as you enter your own
|
||||||
|
|
Loading…
Reference in New Issue