/
pull.go
184 lines (150 loc) · 5.62 KB
/
pull.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cmd
import (
"errors"
date "github.com/mdelapenya/lpn/date"
docker "github.com/mdelapenya/lpn/docker"
internal "github.com/mdelapenya/lpn/internal"
liferay "github.com/mdelapenya/lpn/liferay"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var tagToPull string
var forceRemoval bool
func init() {
rootCmd.AddCommand(pullCmd)
subcommands := []*cobra.Command{pullCE, pullCommerce, pullDXP, pullNightly, pullRelease}
for i := 0; i < len(subcommands); i++ {
subcommand := subcommands[i]
pullCmd.AddCommand(subcommand)
subcommand.Flags().BoolVarP(&forceRemoval, "forceRemoval", "f", false, "Removes the cached, local image, if exists")
subcommand.Flags().StringVarP(&tagToPull, "tag", "t", "", "Sets the image tag to pull")
subcommand.PersistentFlags().BoolVarP(&verbose, "verbose", "V", false, "Runs commands with Debug log level")
subcommand.VisitParents(addVerboseFlag)
}
}
var pullCmd = &cobra.Command{
Use: "pull",
Short: "Pulls a Liferay Portal Docker image",
Long: `Pulls a Liferay Portal Docker image from one of the Official repositories (see configuration file).
For non-official Docker images, the tool pulls from the official repositories (see configuration file)
For that, please run this command adding "ce", "commerce", "dxp", "release" or "nightly" subcommands.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull requires zero or one argument representing the image tag to be pulled")
}
return nil
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
enableDebugLevel()
},
Run: func(cmd *cobra.Command, args []string) {
SubCommandInfo()
},
}
var pullCE = &cobra.Command{
Use: "ce",
Short: "Pulls a Liferay Portal CE Docker image from Official CE repository",
Long: `Pulls a Liferay Portal instance, obtained from the official CE repository.
If no image tag is passed to the command, the default tag (see configuration file) will be used.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull ce requires zero or one argument representing the image tag to be pulled")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if tagToPull == "" {
tagToPull = internal.LpnConfig.GetPortalImageTag("ce")
}
ce := liferay.CE{Tag: tagToPull}
pullDockerImage(ce, forceRemoval)
},
}
var pullCommerce = &cobra.Command{
Use: "commerce",
Short: "Pulls a Liferay Portal Docker image from Commerce Builds",
Long: `Pulls a Liferay Portal Docker image from the Commerce Builds repository".
If no image tag is passed to the command, the default tag (see configuration file) will be used.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull commerce requires zero or one argument representing the image tag to be pulled")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if tagToPull == "" {
tagToPull = internal.LpnConfig.GetPortalImageTag("commerce")
}
commerce := liferay.Commerce{Tag: tagToPull}
pullDockerImage(commerce, forceRemoval)
},
}
var pullDXP = &cobra.Command{
Use: "dxp",
Short: "Pulls a Liferay DXP Docker image from Official DXP repository",
Long: `Pulls a Liferay DXP instance, obtained from the official DXP repository, including a 30-day activation key.
If no image tag is passed to the command, the default tag (see configuration file) will be used.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull ce requires zero or one argument representing the image tag to be pulled")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if tagToPull == "" {
tagToPull = internal.LpnConfig.GetPortalImageTag("dxp")
}
dxp := liferay.DXP{Tag: tagToPull}
pullDockerImage(dxp, forceRemoval)
},
}
var pullNightly = &cobra.Command{
Use: "nightly",
Short: "Pulls a Liferay Portal Docker image from Nightly Builds",
Long: `Pulls a Liferay Portal Docker image from the Nightly Builds repository.
If no image tag is passed to the command, the "master" tag will be used.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull nightly requires zero or one argument representing the image tag to be pulled")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if tagToPull == "" {
tagToPull = date.CurrentDate
}
nightly := liferay.Nightly{Tag: tagToPull}
pullDockerImage(nightly, forceRemoval)
},
}
var pullRelease = &cobra.Command{
Use: "release",
Short: "Pulls a Liferay Portal Docker image from releases",
Long: `Pulls a Liferay Portal instance, obtained from the unofficial releases repository.
If no image tag is passed to the command, the "latest" tag will be used.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("pull nightly requires zero or one argument representing the image tag to be pulled")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if tagToPull == "" {
tagToPull = "latest"
}
release := liferay.Release{Tag: tagToPull}
pullDockerImage(release, forceRemoval)
},
}
// pullDockerImage uses the image interface to pull it from Docker Hub, removing the cached on if
func pullDockerImage(image liferay.Image, forceRemoval bool) {
if forceRemoval {
err := docker.RemoveDockerImage(image.GetFullyQualifiedName())
if err != nil {
log.WithFields(log.Fields{
"image": image.GetFullyQualifiedName(),
}).Info("The image was not found in the local cache. Skipping removal")
}
}
docker.PullDockerImage(image.GetFullyQualifiedName())
}