2017-11-20 11:09:36 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-22 14:27:38 +00:00
|
|
|
api "github.com/fsouza/go-dockerclient"
|
2018-10-13 05:33:53 +00:00
|
|
|
"os"
|
2017-11-20 11:09:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Docker struct {
|
2017-11-22 14:27:38 +00:00
|
|
|
id string
|
|
|
|
client *api.Client
|
2017-11-20 11:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDocker(client *api.Client, id string) *Docker {
|
|
|
|
return &Docker{
|
2017-11-22 14:27:38 +00:00
|
|
|
id: id,
|
|
|
|
client: client,
|
2017-11-20 11:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 05:33:53 +00:00
|
|
|
func (dc *Docker) Exec(cmd []string) error {
|
|
|
|
execCmd, err := dc.client.CreateExec(api.CreateExecOptions{
|
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Cmd: cmd,
|
|
|
|
Container: dc.id,
|
|
|
|
Tty: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dc.client.StartExec(execCmd.ID, api.StartExecOptions{
|
|
|
|
InputStream: os.Stdin,
|
|
|
|
OutputStream: os.Stdout,
|
|
|
|
ErrorStream: os.Stderr,
|
|
|
|
RawTerminal: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-20 11:09:36 +00:00
|
|
|
func (dc *Docker) Start() error {
|
|
|
|
c, err := dc.client.InspectContainer(dc.id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot inspect container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dc.client.StartContainer(c.ID, c.HostConfig); err != nil {
|
|
|
|
return fmt.Errorf("cannot start container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *Docker) Stop() error {
|
|
|
|
if err := dc.client.StopContainer(dc.id, 3); err != nil {
|
|
|
|
return fmt.Errorf("cannot stop container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *Docker) Remove() error {
|
|
|
|
if err := dc.client.RemoveContainer(api.RemoveContainerOptions{ID: dc.id}); err != nil {
|
|
|
|
return fmt.Errorf("cannot remove container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-22 07:41:16 +00:00
|
|
|
|
|
|
|
func (dc *Docker) Pause() error {
|
|
|
|
if err := dc.client.PauseContainer(dc.id); err != nil {
|
|
|
|
return fmt.Errorf("cannot pause container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *Docker) Unpause() error {
|
|
|
|
if err := dc.client.UnpauseContainer(dc.id); err != nil {
|
|
|
|
return fmt.Errorf("cannot unpause container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *Docker) Restart() error {
|
|
|
|
if err := dc.client.RestartContainer(dc.id, 3); err != nil {
|
|
|
|
return fmt.Errorf("cannot restart container: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|