Пример #1
0
func setupConfigsRole(role *ansible.Role, d *Deployment, configs Configs) *ansible.Role {

	role.AddTask(ansible.Task{
		`name`: `Setup rsync user`,
		`set_fact`: ansible.Task{
			`rsync_path`: `rsync`,
		},
		`when`: `ansible_ssh_user == "root"`,
	}).AddTask(ansible.Task{
		`name`: `Setup sudo rsync user`,
		`set_fact`: ansible.Task{
			`rsync_path`: `sudo rsync`,
		},
		`when`: `ansible_ssh_user != "root"`,
	}).AddTask(ansible.Task{
		`name`: `Make Sure rsync is installed`,
		`apt`:  `name=rsync state=present update_cache=yes cache_valid_time=86400`,
	})

	for _, conf := range configs {

		destPath := conf.Path
		if len(destPath) > 0 && destPath[0] != '/' {
			destPath = path.Join(d.Build.RuntimeCfg.Workdir, destPath)
		}
		hash := fmt.Sprintf("%x", sha1.Sum([]byte(conf.Path)))

		role.AddTask(ansible.Task{
			"name": "Create Directory",
			"file": fmt.Sprintf(`path=%s state=directory`, path.Dir(destPath)),
		})

		task := ansible.Task{
			`name`:        `Copy ` + conf.Path,
			`sudo`:        `no`,
			`synchronize`: fmt.Sprintf(`src=%s dest=%s use_ssh_args=yes set_remote_user=yes recursive=yes delete=yes compress=yes mode=push checksum=yes times=no rsync_path={{ rsync_path }}`, hash, destPath),
		}
		if conf.Notify.Service != "" {
			task[`notify`] = fmt.Sprintf("%s %s", conf.Notify.State, conf.Notify.Service)
			role.AddHandler(ansible.Task{
				`name`:    task[`notify`],
				`service`: fmt.Sprintf(`name=%s state=%s`, conf.Notify.Service, conf.Notify.State),
			})
		}

		role.AddTask(task).AddFile(ansible.NewFile(hash, []byte(conf.Content)))
	}

	if d.Build.RuntimeCfg.Workdir != "" {
		role.AddTask(ansible.Task{
			`name`: `Setup Workdir Owner`,
			`file`: fmt.Sprintf(`path=%s recurse=yes group=%s owner=%s`, d.Build.RuntimeCfg.Workdir, d.Build.RuntimeCfg.User, d.Build.RuntimeCfg.User),
		})
	}

	return role
}
Пример #2
0
func setupDockerRole(role *ansible.Role, d *Deployment) *ansible.Role {

	docker := d.Build.RuntimeCfg.Docker

	// Add tasks
	role.AddTask(ansible.Task{
		`name`:        `Check Docker Install`,
		`command`:     `dpkg-query -l docker-engine`,
		`failed_when`: `False`,
		`register`:    `deb_check`,
	}).AddTask(ansible.Task{
		`name`: `Download Docker Setup`,
		`get_url`: ansible.Task{
			`url`:  `https://get.docker.com`,
			`dest`: `/tmp/docker.sh`,
			`mode`: `0755`,
		},
		`when`: `deb_check.stderr.find('no packages found') != -1`,
	}).AddTask(ansible.Task{
		`name`:       `Setup Deps`,
		`apt`:        `name={{ item }} state=present force=yes update_cache=yes cache_valid_time=86400`,
		`with_items`: []string{"python-pip", "apt-transport-https"},
	}).AddTask(ansible.Task{
		`name`:    `Setup Docker`,
		`command`: `sh /tmp/docker.sh`,
		`when`:    `deb_check.stderr.find('no packages found') != -1`,
	}).AddTask(ansible.Task{
		`name`: `Setup docker-py`,
		`pip`:  `name=docker-py state=present`,
	}).AddTask(ansible.Task{
		`name`: `Launch Container`,
		`docker`: ansible.Task{
			`name`:           docker.Name,
			`image`:          docker.Image,
			`pull`:           docker.Pull,
			`restart_policy`: docker.Restart,
			`state`:          `reloaded`,
			`links`:          docker.Links,
			`ports`:          docker.Ports,
			`volumes`:        docker.Volumes,
			// `env`:            docker.Env,
		},
	})

	return role
}
Пример #3
0
func setupStaticRole(role *ansible.Role, d *Deployment) *ansible.Role {

	var files []map[string]string

	for _, s := range d.Build.RuntimeCfg.Static {

		src := path.Join("/karhu/", s.Src)
		dest := path.Join(d.Build.RuntimeCfg.Workdir, s.Dest)

		// files = append(files, fmt.Sprintf(`{ src: "{{ ansible_workdir }}%s", dest: "%s", mode: "%s", user: "******" }`, src, dest, s.Mode, s.User))
		files = append(files, map[string]string{
			"src":  fmt.Sprintf("{{ ansible_workdir }}%s", src),
			"dest": dest,
			"mode": s.Mode,
		})
	}

	role.AddTask(ansible.Task{
		`name`: `Setup rsync user`,
		`set_fact`: ansible.Task{
			`rsync_path`: `rsync`,
		},
		`when`: `ansible_ssh_user == "root"`,
	}).AddTask(ansible.Task{
		`name`: `Setup sudo rsync user`,
		`set_fact`: ansible.Task{
			`rsync_path`: `sudo rsync`,
		},
		`when`: `ansible_ssh_user != "root"`,
	}).AddTask(ansible.Task{
		`name`: `Make Sure rsync is installed`,
		`apt`:  `name=rsync state=present update_cache=yes cache_valid_time=86400`,
	}).AddTask(ansible.Task{
		`name`:        `Copy Files`,
		`sudo`:        `no`,
		`synchronize`: `src={{ item.src }} dest={{ item.dest }} use_ssh_args=yes set_remote_user=yes recursive=yes delete=yes compress=yes mode=push checksum=yes times=no rsync_path={{ rsync_path }}`,
		`with_items`:  files,
	}).AddTask(ansible.Task{
		`name`: `Setup Workdir Owner`,
		`file`: fmt.Sprintf(`path=%s recurse=yes group=%s owner=%s`, d.Build.RuntimeCfg.Workdir, d.Build.RuntimeCfg.User, d.Build.RuntimeCfg.User),
	})

	return role
}
Пример #4
0
func setupDependenciesRole(role *ansible.Role, d *Deployment) *ansible.Role {

	var dependencies []map[string]string

	for _, dep := range d.Build.RuntimeCfg.Dependencies {

		dependencies = append(dependencies, map[string]string{
			"name": dep.Name,
		})
	}

	role.AddTask(ansible.Task{
		`name`:       `Install Dependencies`,
		`apt`:        `name={{ item.name }} state=latest update_cache=yes cache_valid_time=86400`,
		`with_items`: dependencies,
	})

	return role
}
Пример #5
0
func setupBinaryRole(role *ansible.Role, d *Deployment) *ansible.Role {

	// Add tasks
	role.AddTask(ansible.Task{
		`name`: `Setup Dest Dirs`,
		`file`: fmt.Sprintf(`path=%s owner=%s group=%s state=directory`, path.Join(d.Build.RuntimeCfg.Workdir, "/bin"), d.Build.RuntimeCfg.Binary.User, d.Build.RuntimeCfg.Binary.User),
	}).AddTask(ansible.Task{
		`name`:   `Setup Bin`,
		`copy`:   fmt.Sprintf(`src={{ ansible_workdir }}%s dest=%s mode=0755 owner=%s group=%s`, path.Join("/karhu/", d.Build.RuntimeCfg.Binary.Bin), path.Join(d.Build.RuntimeCfg.Workdir, "/bin/", d.Build.RuntimeCfg.Binary.Bin), d.Build.RuntimeCfg.Binary.User, d.Build.RuntimeCfg.Binary.User),
		`notify`: fmt.Sprintf(`restarted %s`, d.Application.Name),
	}).AddTask(ansible.Task{
		`name`:   `Setup systemctl Script`,
		`copy`:   fmt.Sprintf(`src=binary.service dest=/lib/systemd/system/%s.service`, d.Application.Name),
		`notify`: `Reload systemctl daemon`,
	})

	role.AddHandler(ansible.Task{
		`name`:  `Reload systemctl daemon`,
		`shell`: `/bin/systemctl daemon-reload`,
	}).AddHandler(ansible.Task{
		`name`:    fmt.Sprintf(`restarted %s`, d.Application.Name),
		`service`: fmt.Sprintf(`name=%s state=restarted enabled=yes`, d.Application.Name),
	})

	role.AddFile(ansible.NewFile("binary.service", []byte(fmt.Sprintf(serviceFile, d.Application.Name, d.Build.RuntimeCfg.Workdir, d.Build.RuntimeCfg.Binary.User, d.Build.RuntimeCfg.Binary.User, path.Join(d.Build.RuntimeCfg.Workdir, "/bin/", d.Build.RuntimeCfg.Binary.Bin)))))

	return role
}