|
7 | 7 | "regexp" |
8 | 8 | "strings" |
9 | 9 | "time" |
| 10 | + "unicode" |
10 | 11 |
|
11 | 12 | "github.com/yarlson/ftl/pkg/config" |
12 | 13 | ) |
@@ -125,3 +126,96 @@ func (d *Deployment) performHealthChecks(container string, healthCheck *config.H |
125 | 126 |
|
126 | 127 | return fmt.Errorf("container failed to become healthy\n\x1b[93mOutput from the container:\x1b[0m\n%s", grayOutput) |
127 | 128 | } |
| 129 | + |
| 130 | +func (d *Deployment) startContainer(service *config.Service) error { |
| 131 | + _, err := d.runCommand(context.Background(), "docker", "start", service.Name) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("failed to start container for %s: %v", service.Name, err) |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (d *Deployment) createContainer(project string, service *config.Service, suffix string) error { |
| 140 | + svcName := service.Name |
| 141 | + |
| 142 | + args := []string{"run", "-d", "--name", svcName + suffix, "--network", project, "--network-alias", svcName + suffix, "--restart", "unless-stopped"} |
| 143 | + |
| 144 | + for _, value := range service.Env { |
| 145 | + args = append(args, "-e", value) |
| 146 | + } |
| 147 | + |
| 148 | + for _, volume := range service.Volumes { |
| 149 | + if unicode.IsLetter(rune(volume[0])) { |
| 150 | + volume = fmt.Sprintf("%s-%s", project, volume) |
| 151 | + } |
| 152 | + args = append(args, "-v", volume) |
| 153 | + } |
| 154 | + |
| 155 | + if service.HealthCheck != nil { |
| 156 | + args = append(args, "--health-cmd", fmt.Sprintf("curl -sf http://localhost:%d%s || exit 1", service.Port, service.HealthCheck.Path)) |
| 157 | + args = append(args, "--health-interval", fmt.Sprintf("%ds", int(service.HealthCheck.Interval.Seconds()))) |
| 158 | + args = append(args, "--health-retries", fmt.Sprintf("%d", service.HealthCheck.Retries)) |
| 159 | + args = append(args, "--health-timeout", fmt.Sprintf("%ds", int(service.HealthCheck.Timeout.Seconds()))) |
| 160 | + } |
| 161 | + |
| 162 | + for _, port := range service.LocalPorts { |
| 163 | + args = append(args, "-p", fmt.Sprintf("127.0.0.1:%d:%d", port, port)) |
| 164 | + } |
| 165 | + |
| 166 | + if len(service.Forwards) > 0 { |
| 167 | + for _, forward := range service.Forwards { |
| 168 | + args = append(args, "-p", forward) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + hash, err := service.Hash() |
| 173 | + if err != nil { |
| 174 | + return fmt.Errorf("failed to generate config hash: %w", err) |
| 175 | + } |
| 176 | + args = append(args, "--label", fmt.Sprintf("ftl.config-hash=%s", hash)) |
| 177 | + |
| 178 | + if len(service.Entrypoint) > 0 { |
| 179 | + args = append(args, "--entrypoint", strings.Join(service.Entrypoint, " ")) |
| 180 | + } |
| 181 | + |
| 182 | + image := service.Image |
| 183 | + if image == "" { |
| 184 | + image = fmt.Sprintf("%s-%s", project, service.Name) |
| 185 | + } |
| 186 | + args = append(args, image) |
| 187 | + |
| 188 | + if service.Command != "" { |
| 189 | + args = append(args, service.Command) |
| 190 | + } |
| 191 | + |
| 192 | + _, err = d.runCommand(context.Background(), "docker", args...) |
| 193 | + return err |
| 194 | +} |
| 195 | + |
| 196 | +func (d *Deployment) containerShouldBeUpdated(project string, service *config.Service) (bool, error) { |
| 197 | + containerInfo, err := d.getContainerInfo(project, service.Name) |
| 198 | + if err != nil { |
| 199 | + return false, fmt.Errorf("failed to get container info: %w", err) |
| 200 | + } |
| 201 | + |
| 202 | + imageHash, err := d.getImageHash(service.Image) |
| 203 | + if err != nil { |
| 204 | + return false, fmt.Errorf("failed to get image hash: %w", err) |
| 205 | + } |
| 206 | + |
| 207 | + if service.Image == "" && service.ImageUpdated { |
| 208 | + return true, nil |
| 209 | + } |
| 210 | + |
| 211 | + if service.Image != "" && containerInfo.Image != imageHash { |
| 212 | + return true, nil |
| 213 | + } |
| 214 | + |
| 215 | + hash, err := service.Hash() |
| 216 | + if err != nil { |
| 217 | + return false, fmt.Errorf("failed to generate config hash: %w", err) |
| 218 | + } |
| 219 | + |
| 220 | + return containerInfo.Config.Labels["ftl.config-hash"] != hash, nil |
| 221 | +} |
0 commit comments