Beispiel #1
0
func (d *Driver) Create() error {
	if err := d.setUserSubscription(); err != nil {
		return err
	}

	log.Info("Creating Azure machine...")
	vmConfig, err := vmClient.CreateAzureVMConfiguration(d.MachineName, d.Size, d.Image, d.Location)
	if err != nil {
		return err
	}

	log.Debug("Generating certificate for Azure...")
	if err := d.generateCertForAzure(); err != nil {
		return err
	}

	log.Debug("Adding Linux provisioning...")
	vmConfig, err = vmClient.AddAzureLinuxProvisioningConfig(vmConfig, d.GetSSHUsername(), d.UserPassword, d.azureCertPath(), d.SSHPort)
	if err != nil {
		return err
	}

	log.Debug("Authorizing ports...")
	if err := d.addDockerEndpoints(vmConfig); err != nil {
		return err
	}

	log.Debug("Creating VM...")
	if err := vmClient.CreateAzureVM(vmConfig, d.MachineName, d.Location); err != nil {
		return err
	}

	return nil
}
func TestIntegrationLogs(t *testing.T) {

	logsLastH := getLogs(t, "LAST_HOUR")
	logsLast24H := getLogs(t, "LAST_24H")
	logsLast7D := getLogs(t, "LAST_7D")
	logsLast30D := getLogs(t, "LAST_30D")
	logsLast365D := getLogs(t, "LAST_365D")

	for index, _ := range logsLastH {
		assert.Equal(t, logsLastH[index].Id, logsLast24H[index].Id)
		assert.Equal(t, logsLastH[index].Id, logsLast7D[index].Id)
		assert.Equal(t, logsLastH[index].Id, logsLast30D[index].Id)
		assert.Equal(t, logsLastH[index].Id, logsLast365D[index].Id)
	}

	for index, _ := range logsLast24H {
		assert.Equal(t, logsLast24H[index].Id, logsLast7D[index].Id)
		assert.Equal(t, logsLast24H[index].Id, logsLast30D[index].Id)
		assert.Equal(t, logsLast24H[index].Id, logsLast365D[index].Id)
	}

	for index, _ := range logsLast7D {
		assert.Equal(t, logsLast7D[index].Id, logsLast30D[index].Id)
		assert.Equal(t, logsLast7D[index].Id, logsLast365D[index].Id)
	}

	for index, _ := range logsLast30D {
		assert.Equal(t, logsLast30D[index].Id, logsLast365D[index].Id)
	}

	var i = 0
	for index, _ := range logsLast7D {
		i++
		if i == 60 {
			break
		}
		cLog, err := GetAPI().GetLog(logsLast7D[index].Id)
		log.Debug(logsLast7D[index])
		log.Debug(cLog)

		assert.Nil(t, err)
		assert.Equal(t, logsLast7D[index].Id, cLog.Id)
		assert.Equal(t, logsLast7D[index].StartDate, cLog.StartDate)
		assert.Equal(t, logsLast7D[index].EndDate, cLog.EndDate)
		assert.Equal(t, logsLast7D[index].Duration, cLog.Duration)
		assert.Equal(t, logsLast7D[index].Status.Percent, cLog.Status.Percent)
		assert.Equal(t, logsLast7D[index].Status.State, cLog.Status.State)
		assert.Equal(t, logsLast7D[index].Action, cLog.Action)
		assert.Equal(t, logsLast7D[index].Type, cLog.Type)
		assert.Equal(t, logsLast7D[index].Resource.Id, cLog.Resource.Id)
		assert.Equal(t, logsLast7D[index].Resource.Name, cLog.Resource.Name)
		assert.Equal(t, logsLast7D[index].User.Id, cLog.User.Id)
		assert.Equal(t, logsLast7D[index].User.Name, cLog.User.Name)
		assert.Equal(t, logsLast7D[index].CloudPanelId, cLog.CloudPanelId)

		time.Sleep(1 * time.Second)
	}
}
func (d *Driver) hostOnlyIpAvailable() bool {
	ip, err := d.GetIP()
	if err != nil {
		log.Debug("ERROR getting IP: %s", err)
		return false
	}
	if ip != "" {
		log.Debugf("IP is %s", ip)
		return true
	}
	log.Debug("Strangely, there was no error attempting to get the IP, but it was still empty.")
	return false
}
Beispiel #4
0
func NewClient(user string, host string, port int, auth *Auth) (Client, error) {
	sshBinaryPath, err := exec.LookPath("ssh")
	if err != nil {
		log.Debug("SSH binary not found, using native Go implementation")
		return NewNativeClient(user, host, port, auth)
	}

	if defaultClientType == Native {
		log.Debug("Using SSH client type: native")
		return NewNativeClient(user, host, port, auth)
	}

	log.Debug("Using SSH client type: external")
	return NewExternalClient(sshBinaryPath, user, host, port, auth)
}
Beispiel #5
0
func (e *EC2) awsApiCall(v url.Values) (*http.Response, error) {
	v.Set("Version", "2014-06-15")
	log.Debug("Making AWS API call with values:")
	utils.DumpVal(v)
	client := &http.Client{}
	finalEndpoint := fmt.Sprintf("%s?%s", e.Endpoint, v.Encode())
	req, err := http.NewRequest("GET", finalEndpoint, nil)
	if err != nil {
		return &http.Response{}, fmt.Errorf("error creating request from client")
	}
	req.Header.Add("Content-type", "application/json")

	awsauth.Sign4(req, awsauth.Credentials{
		AccessKeyID:     e.Auth.AccessKey,
		SecretAccessKey: e.Auth.SecretKey,
		SecurityToken:   e.Auth.SessionToken,
	})
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("client encountered error while doing the request: %s", err.Error())
		return resp, fmt.Errorf("client encountered error while doing the request: %s", err)
	}

	if resp.StatusCode != http.StatusOK {
		return resp, newAwsApiResponseError(*resp)
	}
	return resp, nil
}
Beispiel #6
0
func (d *Driver) GetIP() (string, error) {
	// DHCP is used to get the IP, so virtualbox hosts don't have IPs unless
	// they are running
	s, err := d.GetState()
	if err != nil {
		return "", err
	}
	if s != state.Running {
		return "", drivers.ErrHostIsNotRunning
	}

	sshClient, err := d.getLocalSSHClient()
	if err != nil {
		return "", err
	}

	output, err := sshClient.Output("ip addr show dev eth1")
	if err != nil {
		log.Debug(output)
		return "", err
	}

	log.Debugf("SSH returned: %s\nEND SSH\n", output)

	// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
	lines := strings.Split(output, "\n")
	for _, line := range lines {
		vals := strings.Split(strings.TrimSpace(line), " ")
		if len(vals) >= 2 && vals[0] == "inet" {
			return vals[1][:strings.Index(vals[1], "/")], nil
		}
	}

	return "", fmt.Errorf("No IP address found %s", output)
}
Beispiel #7
0
func sshAvailableFunc(d Driver) func() bool {
	return func() bool {
		log.Debug("Getting to WaitForSSH function...")
		hostname, err := d.GetSSHHostname()
		if err != nil {
			log.Debugf("Error getting IP address waiting for SSH: %s", err)
			return false
		}
		port, err := d.GetSSHPort()
		if err != nil {
			log.Debugf("Error getting SSH port: %s", err)
			return false
		}
		if err := ssh.WaitForTCP(fmt.Sprintf("%s:%d", hostname, port)); err != nil {
			log.Debugf("Error waiting for TCP waiting for SSH: %s", err)
			return false
		}

		if _, err := RunSSHCommandFromDriver(d, "exit 0"); err != nil {
			log.Debugf("Error getting ssh command 'exit 0' : %s", err)
			return false
		}
		return true
	}
}
Beispiel #8
0
func (provisioner *DebianProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error {
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions

	if provisioner.EngineOptions.StorageDriver == "" {
		provisioner.EngineOptions.StorageDriver = "aufs"
	}

	// HACK: since debian does not come with sudo by default we install
	log.Debug("installing sudo")
	if _, err := provisioner.SSHCommand("if ! type sudo; then apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y sudo; fi"); err != nil {
		return err
	}

	log.Debug("setting hostname")
	if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
		return err
	}

	log.Debug("installing base packages")
	for _, pkg := range provisioner.Packages {
		if err := provisioner.Package(pkg, pkgaction.Install); err != nil {
			return err
		}
	}

	log.Debug("installing docker")
	if err := installDockerGeneric(provisioner, engineOptions.InstallURL); err != nil {
		return err
	}

	log.Debug("waiting for docker daemon")
	if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil {
		return err
	}

	provisioner.AuthOptions = setRemoteAuthOptions(provisioner)

	log.Debug("configuring auth")
	if err := ConfigureAuth(provisioner); err != nil {
		return err
	}

	log.Debug("configuring swarm")
	if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil {
		return err
	}

	// enable in systemd
	log.Debug("enabling docker in systemd")
	if err := provisioner.Service("docker", serviceaction.Enable); err != nil {
		return err
	}

	return nil
}
Beispiel #9
0
func (d *Driver) Restart() error {
	log.Debug("Restarting...")

	if _, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -r now"); err != nil {
		return err
	}

	return nil
}
Beispiel #10
0
func (d *Driver) Kill() error {
	log.Debug("Killing...")

	if _, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -P now"); err != nil {
		return err
	}

	return nil
}
Beispiel #11
0
func DumpVal(vals ...interface{}) {
	for _, val := range vals {
		prettyJSON, err := json.MarshalIndent(val, "", "    ")
		if err != nil {
			log.Fatal(err)
		}
		log.Debug(string(prettyJSON))
	}
}
Beispiel #12
0
func (provisioner *RedHatProvisioner) installOfficialDocker() error {
	log.Debug("installing docker")

	if _, err := provisioner.SSHCommand(fmt.Sprintf("sudo yum install -y --nogpgcheck  %s", provisioner.DockerRPMPath)); err != nil {
		return err
	}

	return nil
}
// GET /dvd_isos/{id}
func (api *API) GetDvdIso(Id string) (*DvdIso, error) {
	log.Debug("requesting information about dvd iso", Id)
	result := new(DvdIso)
	err := api.Client.Get(createUrl(api, "dvd_isos", Id), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	result.api = api
	return result, nil
}
// POST /servers
func (api *API) CreateServer(configuration ServerCreateData) (*Server, error) {
	log.Debug("requesting to create a new server")
	result := new(Server)
	err := api.Client.Post(createUrl(api, "servers"), &configuration, &result, http.StatusAccepted)
	if err != nil {
		return nil, err
	}
	result.api = api
	return result, nil
}
Beispiel #15
0
func (client ExternalClient) Shell() error {
	cmd := exec.Command(client.BinaryPath, client.BaseArgs...)
	log.Debug(cmd)

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	return cmd.Run()
}
Beispiel #16
0
func (d *Driver) securityGroupAvailableFunc(id string) func() bool {
	return func() bool {
		_, err := d.getClient().GetSecurityGroupById(id)
		if err == nil {
			return true
		}
		log.Debug(err)
		return false
	}
}
// GET /server_appliances/{id}
func (api *API) GetServerAppliance(Id string) (*ServerAppliance, error) {
	log.Debug("requesting information about server appliance", Id)
	res := new(ServerAppliance)
	err := api.Client.Get(createUrl(api, "server_appliances", Id), &res, http.StatusOK)
	if err != nil {
		return nil, err
	}
	res.api = api
	return res, nil
}
// DELETE /firewall_policies/{id}
func (fwp *FirewallPolicy) Delete() (*FirewallPolicy, error) {
	log.Debug("Requested to delete firewall policy ", fwp.Id)
	result := new(FirewallPolicy)
	err := fwp.api.Client.Delete(createUrl(fwp.api, "firewall_policies", fwp.Id), &result, http.StatusAccepted)
	if err != nil {
		return nil, err
	}
	result.api = fwp.api
	return result, nil
}
// GET /servers/{id}
func (api *API) GetServer(Id string) (*Server, error) {
	log.Debug("requesting information about server ", Id)
	result := new(Server)
	err := api.Client.Get(createUrl(api, "servers", Id), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	result.api = api
	return result, nil
}
Beispiel #20
0
func (d *Driver) instanceIsRunning() bool {
	st, err := d.GetState()
	if err != nil {
		log.Debug(err)
	}
	if st == state.Running {
		return true
	}
	return false
}
// POST /firewall_policies
func (api *API) CreateFirewallPolicy(configuration FirewallPolicyCreateData) (*FirewallPolicy, error) {
	log.Debug("requesting to create a new firewall policy")
	result := new(FirewallPolicy)
	err := api.Client.Post(createUrl(api, "firewall_policies"), configuration, &result, http.StatusAccepted)
	if err != nil {
		return nil, err
	}
	result.api = api
	return result, nil
}
// PUT /public_ips/{id}
func (ip *PublicIp) UpdateReverseDns(ipAddressConfiguration PublicIpSettings) (*PublicIp, error) {
	log.Debug("updating public ip address '%s'", ip.Id)
	result := new(PublicIp)
	err := ip.api.Client.Put(createUrl(ip.api, PublicIpPathSegment, ip.Id), &ipAddressConfiguration, &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	result.api = ip.api
	return result, nil
}
// POST /public_ips
func (api *API) CreatePublicIp(configuration PublicIpSettings) (*PublicIp, error) {
	log.Debug("Booking a new public ip with type: '%s' and reverse dns: '%s'", configuration.Type, configuration.ReverseDns)
	res := new(PublicIp)
	err := api.Client.Post(createUrl(api, PublicIpPathSegment), &configuration, &res, http.StatusCreated)
	if err != nil {
		return nil, err
	}
	res.api = api
	return res, nil
}
// GET /load_balancers
func (api *API) GetLoadBalancers() ([]LoadBalancer, error) {
	log.Debug("Requesting information about load balancers")
	result := []LoadBalancer{}
	err := api.Client.Get(createUrl(api, "load_balancers"), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	for index, _ := range result {
		result[index].api = api
	}
	return result, nil
}
// GET /shared_storages
func (api *API) GetSharedStorages() ([]SharedStorage, error) {
	log.Debug("Requesting information about shared storages")
	result := []SharedStorage{}
	err := api.Client.Get(createUrl(api, SharedStoragesPathSegment), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	for index, _ := range result {
		result[index].api = api
	}
	return result, nil
}
// GET /dvd_isos
func (api *API) GetDvdIsos() ([]DvdIso, error) {
	log.Debug("requesting information about dvd isos")
	result := []DvdIso{}
	err := api.Client.Get(createUrl(api, "dvd_isos"), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	for index, _ := range result {
		result[index].api = api
	}
	return result, nil
}
Beispiel #27
0
func (d *Driver) instanceIpAvailable() bool {
	ip, err := d.GetIP()
	if err != nil {
		log.Debug(err)
	}
	if ip != "" {
		d.IPAddress = ip
		log.Debugf("Got the IP Address, it's %q", d.IPAddress)
		return true
	}
	return false
}
Beispiel #28
0
func (client ExternalClient) Output(command string) (string, error) {
	args := append(client.BaseArgs, command)

	cmd := exec.Command(client.BinaryPath, args...)
	log.Debug(cmd)

	// Allow piping of local things to remote commands.
	cmd.Stdin = os.Stdin

	output, err := cmd.CombinedOutput()
	return string(output), err
}
// GET /public_ips
func (api *API) GetPublicIps() ([]PublicIp, error) {
	log.Debug("Requesting information about public ips")
	result := []PublicIp{}
	err := api.Client.Get(createUrl(api, PublicIpPathSegment), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	for index, _ := range result {
		result[index].api = api
	}
	return result, nil
}
// GET /private_networks
func (api *API) GetPrivateNetworks() ([]PrivateNetwork, error) {
	log.Debug("Requesting informations about private networks")
	result := []PrivateNetwork{}
	err := api.Client.Get(createUrl(api, PrivateNetworksPathSegment), &result, http.StatusOK)
	if err != nil {
		return nil, err
	}
	for index, _ := range result {
		result[index].api = api
	}
	return result, nil
}