-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathagent.sh
executable file
·150 lines (121 loc) · 3.08 KB
/
agent.sh
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
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
## The script used to start flow.ci agent from docker ##
printHelp()
{
echo ""
echo "Usage: $0 [OPTIONS] COMMAND"
echo ""
echo "Example: ./agent.sh -u http://172.20.10.4:8080 -t token_from_ci_server -m docker start"
echo ""
echo "Options:"
echo -e " -t\t Agent token from ci server"
echo -e " -u\t Server url"
echo -e " -m\t Start agent by <docker> or <binary>"
echo ""
echo "Commands:"
echo -e " start\t start an agent"
echo -e " stop\t stop an agnet"
echo -e " clean\t remove agent container"
echo -e " help\t print help message"
exit 1 # Exit script after printing help
}
checkUrlArg()
{
if [[ ! -n $URL ]]; then
echo "[ERROR] Server url is missing..."
printHelp
fi
}
checkTokenArg()
{
if [[ ! -n $TOKEN ]]; then
echo "[ERROR] Agent token is missing..."
printHelp
fi
}
startAgent()
{
echo $URL
echo $TOKEN
echo $MODEL
if [[ $MODEL == "docker" ]]; then
startFromDocker
elif [[ $MODEL == "binary" ]]; then
startFromBinary
else
echo "[WARN] Agent start model not defined, using default docker agent"
startFromDocker
fi
}
startFromBinary()
{
mkdir -p ./bin
target_bin=./bin/flow-agent-x
if [[ ! -f $target_bin ]]; then
if [[ $OSTYPE == "darwin"* ]]; then
curl -L -o $target_bin https://github.com/FlowCI/flow-agent-x/releases/download/v$AGENT_VERSION/flow-agent-x-mac
elif [[ $OSTYPE == "linux"* ]]; then
curl -L -o $target_bin https://github.com/FlowCI/flow-agent-x/releases/download/v$AGENT_VERSION/flow-agent-x-linux
else
echo "[WARN]: Agent not supported for os $OSTYPE"
exit 1
fi
fi
chmod +x $target_bin
AGENT_HOST_DIR=$HOME/.agent.$TOKEN
mkdir -p $AGENT_HOST_DIR
echo "Starting agent from binary"
$target_bin -u $URL -t $TOKEN -w $AGENT_HOST_DIR -m "name=pyenv,dest=/ci/python,script=init.sh,image=flowci/pyenv,init=init-pyenv-volume.sh"
}
startFromDocker()
{
AGENT_HOST_DIR=$HOME/.agent.$TOKEN
mkdir -p $AGENT_HOST_DIR
if [[ -n $RUNNING_CONTAINER ]]; then
echo "Agent with token $TOKEN is running"
elif [[ -n $EXISTED_CONTAINER ]]; then
echo "Agent with token $TOKEN will restarted"
docker start -i $EXISTED_CONTAINER
else
docker run -it \
--name $CONTAINER_NAME \
-e FLOWCI_SERVER_URL=$URL \
-e FLOWCI_AGENT_TOKEN=$TOKEN \
-e FLOWCI_AGENT_VOLUMES="name=pyenv,dest=/ci/python,script=init.sh,image=flowci/pyenv,init=init-pyenv-volume.sh" \
-e FLOWCI_AGENT_WORKSPACE="/ws" \
-v $AGENT_HOST_DIR:/ws \
-v pyenv:/ci/python \
-v /var/run/docker.sock:/var/run/docker.sock \
flowci/agent:$AGENT_VERSION
fi
}
while getopts ":u:t:m:" arg; do
case $arg in
u) URL=$OPTARG;;
t) TOKEN=$OPTARG;;
m) MODEL=$OPTARG;;
esac
done
AGENT_VERSION=0.21.21
COMMAND="${@: -1}"
CONTAINER_NAME="flowci-agent-$TOKEN"
RUNNING_CONTAINER=$(docker ps -aq -f name=$CONTAINER_NAME -f status=running)
EXISTED_CONTAINER=$(docker ps -aq -f name=$CONTAINER_NAME -f status=exited)
case $COMMAND in
start)
checkUrlArg
checkTokenArg
startAgent
;;
stop)
checkTokenArg
docker stop $CONTAINER_NAME
;;
clean)
checkTokenArg
docker rm -f $CONTAINER_NAME
;;
*)
printHelp
;;
esac