diff --git a/core/compute/compute.go b/core/compute/compute.go old mode 100644 new mode 100755 index 09bceaff..86ecddd0 --- a/core/compute/compute.go +++ b/core/compute/compute.go @@ -106,7 +106,7 @@ func DoInstanceActivate(instance model.Instance, host model.Host, progress *prog containerID := container.ID created := false if containerID == "" { - newID, err := createContainer(dockerClient, &config, &hostConfig, imageTag, instance, name, progress) + newID, err := createContainer(dockerClient, &config, &hostConfig, &networkConfig, imageTag, instance, name, progress) if err != nil { return errors.Wrap(err, constants.DoInstanceActivateError+"failed to create container") } diff --git a/core/compute/compute_common.go b/core/compute/compute_common.go old mode 100644 new mode 100755 index a02392f0..f4732ae8 --- a/core/compute/compute_common.go +++ b/core/compute/compute_common.go @@ -24,7 +24,7 @@ import ( "time" ) -func createContainer(dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, +func createContainer(dockerClient *client.Client, config *container.Config, hostConfig *container.HostConfig, networkConfig *network.NetworkingConfig, imageTag string, instance model.Instance, name string, progress *progress.Progress) (string, error) { labels := config.Labels if labels[constants.PullImageLabels] == "always" { @@ -43,13 +43,13 @@ func createContainer(dockerClient *client.Client, config *container.Config, host dockerImage := utils.ParseRepoTag(imageTag) config.Image = dockerImage.UUID - containerResponse, err := dockerClient.ContainerCreate(context.Background(), config, hostConfig, nil, name) + containerResponse, err := dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkConfig, name) // if image doesn't exist if client.IsErrImageNotFound(err) { if err := storage.PullImage(instance.Image, progress, dockerClient, imageTag); err != nil { return "", errors.Wrap(err, constants.CreateContainerError+"failed to pull image") } - containerResponse, err1 := dockerClient.ContainerCreate(context.Background(), config, hostConfig, nil, name) + containerResponse, err1 := dockerClient.ContainerCreate(context.Background(), config, hostConfig, networkConfig, name) if err1 != nil { return "", errors.Wrap(err1, constants.CreateContainerError+"failed to create container") } @@ -253,7 +253,6 @@ func setupLegacyCommand(config *container.Config, fields model.InstanceFields, c } func setupNetworkingConfig(networkConfig *network.NetworkingConfig, instance model.Instance) { - } func setupLabels(labels map[string]string, config *container.Config) { diff --git a/core/compute/compute_record.go b/core/compute/compute_record.go index 6af4246f..419317b2 100644 --- a/core/compute/compute_record.go +++ b/core/compute/compute_record.go @@ -45,7 +45,7 @@ func RecordState(client *client.Client, instance model.Instance, dockerID string } if ok := utils.IsPathExist(contDir); !ok { - mkErr := os.MkdirAll(contDir, 777) + mkErr := os.MkdirAll(contDir, 644) if mkErr != nil { return errors.Wrap(mkErr, constants.RecordStateError+"failed to make directory") } diff --git a/core/compute/compute_unix.go b/core/compute/compute_unix.go old mode 100644 new mode 100755 diff --git a/core/compute/compute_windows.go b/core/compute/compute_windows.go old mode 100644 new mode 100755 index 72647112..c7645306 --- a/core/compute/compute_windows.go +++ b/core/compute/compute_windows.go @@ -41,3 +41,7 @@ func setupFieldsHostConfig(fields model.InstanceFields, hostConfig *container.Ho func setupDeviceOptions(hostConfig *container.HostConfig, instance model.Instance, infoData model.InfoData) { } + +func setupComputeResourceFields(hostConfig *container.HostConfig, instance model.Instance) { + +} diff --git a/core/hostInfo/cpu_info.go b/core/hostInfo/cpu_info.go old mode 100644 new mode 100755 index 9046d725..a4a1606e --- a/core/hostInfo/cpu_info.go +++ b/core/hostInfo/cpu_info.go @@ -1,5 +1,3 @@ -// +build linux freebsd solaris openbsd darwin - package hostInfo import ( diff --git a/core/hostInfo/disk_info.go b/core/hostInfo/disk_info.go old mode 100644 new mode 100755 index 00c36f41..902de91a --- a/core/hostInfo/disk_info.go +++ b/core/hostInfo/disk_info.go @@ -1,5 +1,3 @@ -// +build linux freebsd solaris openbsd darwin - package hostInfo import ( diff --git a/core/hostInfo/os_c.go b/core/hostInfo/os_c.go old mode 100644 new mode 100755 index 8785c686..c14d10e0 --- a/core/hostInfo/os_c.go +++ b/core/hostInfo/os_c.go @@ -62,7 +62,7 @@ func (o OSCollector) KeyName() string { func (o OSCollector) getOS(infoData model.InfoData) (map[string]string, error) { data := map[string]string{} data["operatingSystem"] = infoData.Info.OperatingSystem - kernelVersion, err := utils.GetKernelVersion() + kernelVersion, err := getKernelVersion() if err != nil { return map[string]string{}, errors.Wrap(err, constants.GetOSError+"failed to get kernel version") } diff --git a/core/hostInfo/os_c_unix.go b/core/hostInfo/os_c_unix.go new file mode 100755 index 00000000..695970f9 --- /dev/null +++ b/core/hostInfo/os_c_unix.go @@ -0,0 +1,27 @@ +//+build !windows + +package hostInfo + +import ( + "bufio" + "github.com/pkg/errors" + "github.com/rancher/agent/utilities/constants" + "os" + "regexp" +) + +func getKernelVersion() (string, error) { + file, err := os.Open("/proc/version") + defer file.Close() + data := []string{} + if err != nil { + return "", errors.Wrap(err, constants.GetKernelVersionError+"failed to open process version file") + } + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + data = append(data, scanner.Text()) + } + version := regexp.MustCompile("\\d+.\\d+.\\d+").FindString(data[0]) + return version, nil +} diff --git a/core/hostInfo/os_c_windows.go b/core/hostInfo/os_c_windows.go new file mode 100755 index 00000000..461602de --- /dev/null +++ b/core/hostInfo/os_c_windows.go @@ -0,0 +1,7 @@ +//+build windows + +package hostInfo + +func getKernelVersion() (string, error) { + return "", nil +} diff --git a/core/storage/storage.go b/core/storage/storage.go old mode 100644 new mode 100755 index cfae62f3..ba697217 --- a/core/storage/storage.go +++ b/core/storage/storage.go @@ -2,6 +2,7 @@ package storage import ( "fmt" + "github.com/Sirupsen/logrus" engineCli "github.com/docker/engine-api/client" "github.com/docker/engine-api/types" diff --git a/handlers/common.go b/handlers/common.go old mode 100644 new mode 100755 diff --git a/service/hostapi/path_windows.go b/service/hostapi/path_windows.go index 70c245ff..a6561aba 100644 --- a/service/hostapi/path_windows.go +++ b/service/hostapi/path_windows.go @@ -1,3 +1,3 @@ package hostapi -const execPath string = "c:\\agent\\src\\github.com\\rancher\\host-api\\host-api.exe" +const execPath string = "c:\\host-api.exe" diff --git a/trash.yml b/trash.yml index d050fd83..caec2018 100755 --- a/trash.yml +++ b/trash.yml @@ -56,7 +56,6 @@ import: - package: github.com/shirou/gopsutil version: af2b5127ea65bc380b2e07f2f27829aac2640d65 - - package: github.com/ShowMax/go-fqdn version: 2501cdd51ef4c60dd727c58b2199e1a09466b10f diff --git a/utilities/docker/launch_windows.go b/utilities/docker/launch_windows.go old mode 100644 new mode 100755 index f49589c0..cc711eb1 --- a/utilities/docker/launch_windows.go +++ b/utilities/docker/launch_windows.go @@ -3,16 +3,53 @@ package docker import ( "fmt" "github.com/docker/engine-api/client" + dclient "github.com/docker/engine-api/client" + "github.com/docker/go-connections/tlsconfig" "github.com/pkg/errors" "github.com/rancher/agent/utilities/constants" + "net/http" "os" + "path/filepath" + "time" ) func launchDefaultClient(version string) (*client.Client, error) { - ip := fmt.Sprintf("tcp://%v:2375", os.Getenv("CATTLE_AGENT_IP")) + ip := fmt.Sprintf("tcp://%v:2375", os.Getenv("DEFAULT_GATEWAY")) cliFromAgent, cerr := client.NewClient(ip, version, nil, nil) if cerr != nil { return nil, errors.Wrap(cerr, constants.LaunchDefaultClientError) } return cliFromAgent, nil } + +func NewEnvClientWithTimeout(timeout time.Duration) (*dclient.Client, error) { + var client *http.Client + if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" { + options := tlsconfig.Options{ + CAFile: filepath.Join(dockerCertPath, "ca.pem"), + CertFile: filepath.Join(dockerCertPath, "cert.pem"), + KeyFile: filepath.Join(dockerCertPath, "key.pem"), + InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "", + } + tlsc, err := tlsconfig.Client(options) + if err != nil { + return nil, err + } + + client = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: tlsc, + }, + Timeout: timeout, + } + } + + host := fmt.Sprintf("tcp://%v:2375", os.Getenv("DEFAULT_GATEWAY")) + + version := os.Getenv("DOCKER_API_VERSION") + if version == "" { + version = dclient.DefaultVersion + } + + return dclient.NewClient(host, version, client, nil) +} diff --git a/utilities/utils/response.go b/utilities/utils/response.go old mode 100644 new mode 100755 index 4332d4f3..adea958f --- a/utilities/utils/response.go +++ b/utilities/utils/response.go @@ -90,7 +90,7 @@ func getInstanceHostMapData(event *revents.Event, client *client.Client) (map[st if err != nil { return map[string]interface{}{}, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to get mount data") } - dockerIP := inspect.NetworkSettings.IPAddress + dockerIP := getIP(inspect) if container.Ports != nil && len(container.Ports) > 0 { for _, port := range container.Ports { privatePort := fmt.Sprintf("%v/%v", port.PrivatePort, port.Type) diff --git a/utilities/utils/response_unix.go b/utilities/utils/response_unix.go new file mode 100755 index 00000000..f739fc78 --- /dev/null +++ b/utilities/utils/response_unix.go @@ -0,0 +1,11 @@ +//+build !windows + +package utils + +import ( + "github.com/docker/engine-api/types" +) + +func getIP(inspect types.ContainerJSON) string { + return inspect.NetworkSettings.IPAddress +} diff --git a/utilities/utils/response_windows.go b/utilities/utils/response_windows.go new file mode 100755 index 00000000..41a7e460 --- /dev/null +++ b/utilities/utils/response_windows.go @@ -0,0 +1,51 @@ +//+build windows + +package utils + +import ( + "bufio" + "github.com/Sirupsen/logrus" + "github.com/docker/engine-api/types" + "github.com/rancher/agent/utilities/constants" + "github.com/rancher/agent/utilities/docker" + "golang.org/x/net/context" + "regexp" + "strings" + "time" +) + +func getIP(inspect types.ContainerJSON) string { + containerID := inspect.ID + client := docker.GetClient(constants.DefaultVersion) + execConfig := types.ExecConfig{ + AttachStdout: true, + AttachStdin: true, + AttachStderr: true, + Privileged: true, + Tty: false, + Detach: false, + Cmd: []string{"powershell", "ipconfig"}, + } + ip := "" + // waiting for the DHCP to assign IP address. Testing purpose. May try multiple times until ip address arrives + time.Sleep(time.Duration(2) * time.Second) + execObj, err := client.ContainerExecCreate(context.Background(), containerID, execConfig) + if err != nil { + logrus.Error(err) + return "" + } + hijack, err := client.ContainerExecAttach(context.Background(), execObj.ID, execConfig) + if err != nil { + logrus.Error(err) + return "" + } + scanner := bufio.NewScanner(hijack.Reader) + for scanner.Scan() { + output := scanner.Text() + if strings.Contains(output, "IPv4 Address") { + ip = regexp.MustCompile("(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$").FindString(output) + } + } + hijack.Close() + return ip +} diff --git a/utilities/utils/utils.go b/utilities/utils/utils.go old mode 100644 new mode 100755 index 4a6a9ca8..204a8b90 --- a/utilities/utils/utils.go +++ b/utilities/utils/utils.go @@ -1,7 +1,6 @@ package utils import ( - "bufio" "encoding/json" "fmt" engineCli "github.com/docker/engine-api/client" @@ -454,22 +453,6 @@ func SemverTrunk(version string, vals int) string { return version } -func GetKernelVersion() (string, error) { - file, err := os.Open("/proc/version") - defer file.Close() - data := []string{} - if err != nil { - return "", errors.Wrap(err, constants.GetKernelVersionError+"failed to open process version file") - } - scanner := bufio.NewScanner(file) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - data = append(data, scanner.Text()) - } - version := regexp.MustCompile("\\d+.\\d+.\\d+").FindString(data[0]) - return version, nil -} - func NameFilter(name string, container types.Container) bool { names := container.Names if names == nil || len(names) == 0 { diff --git a/vendor/github.com/docker/engine-api/client/image_pull.go b/vendor/github.com/docker/engine-api/client/image_pull.go old mode 100644 new mode 100755 index e2c49ec5..56b550e3 --- a/vendor/github.com/docker/engine-api/client/image_pull.go +++ b/vendor/github.com/docker/engine-api/client/image_pull.go @@ -32,7 +32,11 @@ func (cli *Client) ImagePull(ctx context.Context, ref string, options types.Imag } resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth) - if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil { + flag := false + if cli.ClientVersion() != "1.23" && cli.ClientVersion() != "1.24" { + flag = true + } + if (resp.statusCode == http.StatusUnauthorized || flag) && options.PrivilegeFunc != nil { newAuthHeader, privilegeErr := options.PrivilegeFunc() if privilegeErr != nil { return nil, privilegeErr diff --git a/vendor/github.com/nu7hatch/gouuid/uuid.go b/vendor/github.com/nu7hatch/gouuid/uuid.go index ac9623b7..ca960aa9 100644 --- a/vendor/github.com/nu7hatch/gouuid/uuid.go +++ b/vendor/github.com/nu7hatch/gouuid/uuid.go @@ -16,7 +16,7 @@ import ( "regexp" ) -// The UUID reserved variants. +// The UUID reserved variants. const ( ReservedNCS byte = 0x80 ReservedRFC4122 byte = 0x40 diff --git a/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go b/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go index 71d38842..e7881a5c 100644 --- a/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go +++ b/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go @@ -97,7 +97,7 @@ func finishCPUInfo(c *InfoStat) error { return nil } } - c.Mhz = value/1000.0 // value is in kHz + c.Mhz = value / 1000.0 // value is in kHz return nil } diff --git a/vendor/gopkg.in/check.v1/benchmark.go b/vendor/gopkg.in/check.v1/benchmark.go index 46ea9dc6..b2d35194 100644 --- a/vendor/gopkg.in/check.v1/benchmark.go +++ b/vendor/gopkg.in/check.v1/benchmark.go @@ -1,9 +1,9 @@ // Copyright (c) 2012 The Go Authors. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: -// +// // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above @@ -13,7 +13,7 @@ // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. -// +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR