Skip to content

Commit f0c9fe5

Browse files
committed
Add support to run the generated script with interactive bash (close #1)
1 parent 6cc2b9b commit f0c9fe5

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,62 @@ if /usr/bin/docker ps | grep --quiet myapp-api_1 ; then
4444
/usr/bin/docker rm -f myapp-api_1
4545
fi
4646

47-
/usr/bin/docker run \
48-
--privileged=true \
47+
/usr/bin/docker run \
4948
--restart=always \
5049
-d \
5150
--name myapp-api_1 \
5251
-v .:/src \
52+
--link myapp-redis_1:redis \
5353
-e MONGO_DATABASE="develop_api" -e NODE_ENV="development" -e VIRTUAL_HOST="api.mydomain.com" -e VIRTUAL_PORT="3000" \
5454
-p 3000 \
55-
docker.mydomain.com/api:latest npm start
55+
docker.mydomain.com/api:latest npm start
56+
```
57+
58+
**output: myapp-api.1.sh** with `-interactive-bash`
59+
```bash
60+
#!/bin/bash
61+
/usr/bin/docker pull docker.mydomain.com/api:latest
62+
63+
if /usr/bin/docker ps -a | grep --quiet myapp-api_1 ; then
64+
/usr/bin/docker rm -f myapp-api_1
65+
fi
66+
67+
68+
while [ "$#" -gt 0 ]; do case "$1" in
69+
--interactive-bash) interactivebash="true"; shift 1;;
70+
*) shift;;
71+
esac
72+
done
73+
74+
if [[ $interactivebash == "true" ]]; then
75+
/usr/bin/docker run \
76+
-ti \
77+
--name myapp-api_1 \
78+
-v .:/src \
79+
--link myapp-redis_1:redis \
80+
-e MONGO_DATABASE="develop_api" -e NODE_ENV="development" -e VIRTUAL_HOST="api.mydomain.com" -e VIRTUAL_PORT="3000" \
81+
-p 3000 \
82+
docker.mydomain.com/api:latest bash
83+
else
84+
/usr/bin/docker run \
85+
--restart=always \
86+
-d \
87+
--name myapp-api_1 \
88+
-v .:/src \
89+
--link myapp-redis_1:redis \
90+
-e MONGO_DATABASE="develop_api" -e NODE_ENV="development" -e VIRTUAL_HOST="api.mydomain.com" -e VIRTUAL_PORT="3000" \
91+
-p 3000 \
92+
docker.mydomain.com/api:latest npm start
93+
fi
5694
```
5795

5896
## Options
5997

6098
- **-app**: Application name
6199
- **-output**: Output directory (default `.`)
62100
- **-yml**: Compose file path (default `docker-compose.yml`)
63-
- **docker-host**: Docker host connection
101+
- **-docker-host**: Docker host connection
102+
- **-interactive-bash**: Include option to run the generated script with interactive bash. Running the generated script without any argument will executed it normally. But running it with `--interactive-bash` will execute the container with interactive bash. Pretty handful for debug.
64103

65104

66105

main.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
)
1616

1717
var (
18-
appName string
19-
composePath string
20-
outputPath string
21-
dockerHostConn string
18+
appName string
19+
composePath string
20+
outputPath string
21+
dockerHostConn string
22+
interactiveBash bool
2223
)
2324

2425
const bashTemplate = `#!/bin/bash
@@ -28,6 +29,38 @@ if /usr/bin/docker {{.DockerHostConnCmdArg}} ps -a | grep --quiet {{.Service.Nam
2829
/usr/bin/docker {{.DockerHostConnCmdArg}} rm -f {{.Service.Name}}_1
2930
fi
3031
32+
{{if .InteractiveBash}}
33+
while [ "$#" -gt 0 ]; do case "$1" in
34+
--interactive-bash) interactivebash="true"; shift 1;;
35+
*) shift;;
36+
esac
37+
done
38+
39+
if [[ $interactivebash == "true" ]]; then
40+
/usr/bin/docker {{.DockerHostConnCmdArg}} run \
41+
{{if .Service.Privileged}}--privileged=true {{end}} \
42+
-ti \
43+
--name {{.Service.Name}}_1 \
44+
{{if .Service.HostName}}--hostname={{.Service.HostName}} {{end}} \
45+
{{range .Service.Volumes}}-v {{.}} {{end}} \
46+
{{range .Service.Links}}--link {{.}} {{end}} \
47+
{{range $key, $value := .Service.Environment}}-e {{$key}}="{{$value}}" {{end}} \
48+
{{range .Service.Ports}}-p {{.}} {{end}} \
49+
{{.Service.Image}} bash
50+
else
51+
/usr/bin/docker {{.DockerHostConnCmdArg}} run \
52+
{{if .Service.Privileged}}--privileged=true {{end}} \
53+
--restart=always \
54+
-d \
55+
--name {{.Service.Name}}_1 \
56+
{{if .Service.HostName}}--hostname={{.Service.HostName}} {{end}} \
57+
{{range .Service.Volumes}}-v {{.}} {{end}} \
58+
{{range .Service.Links}}--link {{.}} {{end}} \
59+
{{range $key, $value := .Service.Environment}}-e {{$key}}="{{$value}}" {{end}} \
60+
{{range .Service.Ports}}-p {{.}} {{end}} \
61+
{{.Service.Image}} {{.Service.Command}}
62+
fi
63+
{{else}}
3164
/usr/bin/docker {{.DockerHostConnCmdArg}} run \
3265
{{if .Service.Privileged}}--privileged=true {{end}} \
3366
--restart=always \
@@ -38,13 +71,15 @@ fi
3871
{{range .Service.Links}}--link {{.}} {{end}} \
3972
{{range $key, $value := .Service.Environment}}-e {{$key}}="{{$value}}" {{end}} \
4073
{{range .Service.Ports}}-p {{.}} {{end}} \
41-
{{.Service.Image}} {{.Service.Command}}
74+
{{.Service.Image}} {{.Service.Command}}
75+
{{end}}
4276
`
4377

4478
// ScriptDataTemplate contains the whole data configuration used to fill the script
4579
type ScriptDataTemplate struct {
4680
AppName string
4781
DockerHostConnCmdArg string
82+
InteractiveBash bool
4883
Service Service
4984
}
5085

@@ -100,7 +135,11 @@ func removeBlankLinkes(path string) {
100135

101136
func buildScriptDataTemplate(serviceName string, service Service) ScriptDataTemplate {
102137
// common data template for all services from the same app
103-
data := ScriptDataTemplate{AppName: appName}
138+
data := ScriptDataTemplate{
139+
AppName: appName,
140+
InteractiveBash: interactiveBash,
141+
}
142+
104143
if dockerHostConn != "" {
105144
data.DockerHostConnCmdArg = "--host=" + dockerHostConn
106145
}
@@ -140,6 +179,7 @@ func main() {
140179
flag.StringVar(&composePath, "yml", "docker-compose.yml", "compose file path")
141180
flag.StringVar(&outputPath, "output", ".", "output directory")
142181
flag.StringVar(&dockerHostConn, "docker-host", "", "docker host connection")
182+
flag.BoolVar(&interactiveBash, "interactive-bash", false, "include option to run the generated script with interactive bash")
143183

144184
flag.Parse()
145185

0 commit comments

Comments
 (0)