-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.sls
137 lines (123 loc) · 3.89 KB
/
lib.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{% macro go_build_from_git(build_config) %}
{# config:
source:
repo:
rev:
build:
# salt['extutils.re_sub']('([a-z]+)://[([^@]@)?([^:]:[0-9]+)?/(.+)\.git', '\\4', config.source.repo)
name: default: last part of \\4
dir: default: \\4
rev: default: config.source.rev
make: default: 'go build' + config.build.dir
check:
bin_files: []
target:
versiondir: /usr/local/src
symlinkdir: /usr/local/bin
#}
{% load_yaml as defaults %}
user: builder
build:
name:
dir:
rev:
make:
cleanup: false
target:
versiondir: /usr/local/src
symlinkdir: /usr/local/bin
{% endload %}
{% set config=salt['grains.filter_by']({'none': defaults },
grain='none', default= 'none', merge= build_config) %}
{% set gitrepo_regex='([a-z]+)://([^@]+@)?([^:/]+)(:[0-9]+)?/(.+)\.git' %}
{% set dir_from_git= salt['extutils.re_sub'](gitrepo_regex, '\\3/\\5', config.source.repo) %}
{% if config.build.dir is none %} {% do config.build.update({'dir': dir_from_git}) %}{% endif %}
{% if config.build.name is none %}{% do config.build.update({'name': dir_from_git.split('/')[-1]}) %}{% endif %}
{% if config.build.rev is none %} {% do config.build.update({'rev': config.source.rev}) %}{% endif %}
{% if config.build.make is none %}{% do config.build.update({'make': 'go build ' + config.build.dir}) %}{% endif %}
{% set home="/home/"+ config.user %}
{% set gopath=home+ "/go" %}
{% set target_versiondir= config.target.versiondir+ '/'+ config.build.name+ '-'+ config.build.rev+ '-'+
grains['kernel']|lower+ '-'+ grains['osarch']|lower %}
go_profile-{{ config.build.name }}:
file.managed:
- name: {{ home }}/.profile
- user: {{ config.user }}
- group: {{ config.user }}
- require:
- user: go-build-{{ config.build.name }}
go_profile-activate-{{ config.build.name }}:
file.append:
- name: {{ home }}/.profile
- text: |
export GOPATH={{ gopath }}
export PATH=${GOPATH}/bin:$PATH
- require:
- file: go_profile-{{ config.build.name }}
go-build-{{ config.build.name }}:
group:
- present
- name: {{ config.user }}
user:
- present
- name: {{ config.user }}
- gid: {{ config.user }}
- home: {{ home }}
- shell: /bin/bash
- remove_groups: False
- require:
- group: go-build-{{ config.build.name }}
file.directory:
- name: {{ gopath }}/bin
- user: {{ config.user }}
- group: {{ config.user }}
- mode: 755
- makedirs: True
- require:
- user: go-build-{{ config.build.name }}
git.latest:
- name: {{ config.source.repo }}
{%- if config.source.rev is defined and config.source.rev != none %}
- rev: {{ config.source.rev }}
{%- endif %}
- target: {{ gopath }}/src/{{ config.build.dir }}
- user: {{ config.user }}
- submodules: True
- require:
- file: go-build-{{ config.build.name }}
- pkg: golang
cmd.wait:
- cwd: {{ gopath }}/bin
- env:
- GOPATH: "{{ gopath }}"
- name: {{ config.build.make }}
- user: {{ config.user }}
- group: {{ config.user }}
- onlyif:
- test ! -f {{ target_versiondir}}/{{ config.build.name }}
- require:
- file: go_profile-activate-{{ config.build.name }}
- watch:
- git: go-build-{{ config.build.name }}
{% for n in config.build.bin_files %}
"go-deploy-{{ config.build.name }}-{{ n }}":
cmd.run:
- name: |
if test ! -d {{ target_versiondir }}; then
mkdir -p {{ target_versiondir }}
fi
cp -a -f -t {{ target_versiondir }} {{ gopath }}/bin/{{ n }}
- watch:
- cmd: go-build-{{ config.build.name }}
- require_in:
- cmd: "go-deploy-{{ config.build.name }}-symlinks"
{% endfor %}
go-deploy-{{ config.build.name }}-symlinks:
cmd.run:
- name: |
for n in `ls {{ target_versiondir }}`; do
ln -s -f -T {{ target_versiondir }}/$n {{ config.target.symlinkdir }}/$n
done
- watch:
- cmd: go-build-{{ config.build.name }}
{% endmacro %}