func UpdateAnsibleRC(inventoryFile string, hosts string, c *client.Client, ns string, rcFile string) (*api.ReplicationController, error) { rcConfig, err := k8s.ReadReplicationControllerFromFile(rcFile) if err != nil { return nil, err } gitUrl, err := findGitUrl() if err != nil { return nil, err } if len(gitUrl) == 0 { return nil, fmt.Errorf("Could not find git URL in git configu file %s", gitConfig) } podSpec := k8s.GetOrCreatePodSpec(rcConfig) container := k8s.GetFirstContainerOrCreate(rcConfig) if len(container.Image) == 0 { container.Image = "fabric8/gosupervise" } if len(container.Name) == 0 { container.Name = "gosupervise" } if len(container.ImagePullPolicy) == 0 { container.ImagePullPolicy = "IfNotPresent" } k8s.EnsureContainerHasEnvVar(container, EnvHosts, hosts) command := k8s.GetContainerEnvVar(container, EnvCommand) if len(command) == 0 { return nil, fmt.Errorf("No environemnt variable value defined for %s in ReplicationController YAML file %s", EnvCommand, rcFile) } volumeName := "playbook-volume" k8s.EnsurePodSpecHasGitVolume(podSpec, volumeName, gitUrl, "master") k8s.EnsureContainerHasGitVolumeMount(container, volumeName, PlaybookVolumeMount) hostEntries, err := LoadHostEntries(inventoryFile, hosts) if err != nil { return nil, err } log.Info("Found %d host entries in the Ansible inventory for %s", len(hostEntries), hosts) log.Info("Using git URL %s", gitUrl) rcName := rcConfig.ObjectMeta.Name isUpdate := true rc, err := c.ReplicationControllers(ns).Get(rcName) if err != nil { isUpdate = false rc = &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Namespace: ns, Name: rcName, }, } } pods, err := c.Pods(ns).List(nil, nil) if err != nil { return nil, err } // merge the RC configuration to allow configuration rc.Spec = rcConfig.Spec metadata := rc.ObjectMeta resourceVersion := metadata.ResourceVersion annotations := metadata.Annotations rcSpec := &rc.Spec rcSpec.Replicas = len(hostEntries) log.Info("found RC with name %s and version %s and replicas %d", rcName, resourceVersion, rcSpec.Replicas) deletePodsForOldHosts(c, ns, annotations, pods, hostEntries) replicationController := c.ReplicationControllers(ns) if isUpdate { _, err = replicationController.Update(rc) } else { _, err = replicationController.Create(rc) } if err != nil { log.Info("Failed to update the RC, could be concurrent update failure: %s", err) return nil, err } return rc, nil }
func runAnsiblePod(c *cli.Context) { args := c.Args() if len(args) < 2 { log.Die("Expected at least 2 arguments!") } hosts := args[0] command := strings.Join(args[1:], " ") log.Info("running command on a host from %s and command `%s`", hosts, command) f := cmdutil.NewFactory(nil) if f == nil { log.Die("Failed to create Kuberentes client factory!") } kubeclient, _ := f.Client() if kubeclient == nil { log.Die("Failed to create Kuberentes client!") } ns, _, _ := f.DefaultNamespace() if len(ns) == 0 { ns = "default" } rcFile, err := osExpandAndVerify(c, "rc") if err != nil { fail(err) } port, err := osExpandAndVerifyGlobal(c, "port") if err != nil { fail(err) } inventory, err := osExpandAndVerify(c, "inventory") if err != nil { fail(err) } rc, err := k8s.ReadReplicationControllerFromFile(rcFile) if err != nil { fail(err) } rcName := rc.ObjectMeta.Name if len(rcName) == 0 { log.Die("No ReplicationController name in the yaml file %s", rcFile) } hostEntry, err := ansible.ChooseHostAndPrivateKey(inventory, hosts, kubeclient, ns, rcName) if err != nil { fail(err) } host := hostEntry.Host user := hostEntry.User useWinRM := c.Bool("winrm") || hostEntry.UseWinRM if useWinRM { log.Info("Using WinRM to connect to the hosts %s", hosts) password, err := osExpandAndVerify(c, "password") if err != nil { fail(err) } err = winrm.RemoteWinRmCommand(user, password, host, port, command) } else { privatekey := hostEntry.PrivateKey hostPort := host + ":" + port err = ssh.RemoteSshCommand(user, privatekey, hostPort, command) } if err != nil { log.Err("Failed: %v", err) } }