|
| 1 | +// Copyright 2019 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "log" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "github.com/operator-framework/operator-sdk/internal/util/projutil" |
| 23 | + "github.com/operator-framework/operator-sdk/pkg/scaffold" |
| 24 | + "github.com/operator-framework/operator-sdk/pkg/scaffold/ansible" |
| 25 | + "github.com/operator-framework/operator-sdk/pkg/scaffold/input" |
| 26 | + |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +// NewMigrateCmd returns a command that will add source code to an existing non-go operator |
| 31 | +func NewMigrateCmd() *cobra.Command { |
| 32 | + return &cobra.Command{ |
| 33 | + Use: "migrate", |
| 34 | + Short: "Adds source code to an operator", |
| 35 | + Long: `operator-sdk migrate adds a main.go source file and any associated source files for an operator that is not of the "go" type.`, |
| 36 | + Run: migrateRun, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// migrateRun determines the current operator type and runs the corresponding |
| 41 | +// migrate function. |
| 42 | +func migrateRun(cmd *cobra.Command, args []string) { |
| 43 | + projutil.MustInProjectRoot() |
| 44 | + |
| 45 | + _ = projutil.CheckAndGetProjectGoPkg() |
| 46 | + |
| 47 | + opType := projutil.GetOperatorType() |
| 48 | + switch opType { |
| 49 | + case projutil.OperatorTypeAnsible: |
| 50 | + migrateAnsible() |
| 51 | + default: |
| 52 | + log.Fatalf("operator of type %s cannot be migrated.", opType) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// migrateAnsible runs the migration process for an ansible-based operator |
| 57 | +func migrateAnsible() { |
| 58 | + wd := projutil.MustGetwd() |
| 59 | + |
| 60 | + cfg := &input.Config{ |
| 61 | + AbsProjectPath: wd, |
| 62 | + ProjectName: filepath.Base(wd), |
| 63 | + } |
| 64 | + |
| 65 | + dockerfile := ansible.DockerfileHybrid{ |
| 66 | + Watches: true, |
| 67 | + Roles: true, |
| 68 | + } |
| 69 | + _, err := os.Stat("playbook.yaml") |
| 70 | + switch { |
| 71 | + case err == nil: |
| 72 | + dockerfile.Playbook = true |
| 73 | + case os.IsNotExist(err): |
| 74 | + log.Print("No playbook was found, so not including it in the new Dockerfile") |
| 75 | + default: |
| 76 | + log.Fatalf("error trying to stat playbook.yaml: (%v)", err) |
| 77 | + } |
| 78 | + |
| 79 | + dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile) |
| 80 | + newDockerfilePath := dockerfilePath + ".sdkold" |
| 81 | + err = os.Rename(dockerfilePath, newDockerfilePath) |
| 82 | + if err != nil { |
| 83 | + log.Fatalf("failed to rename Dockerfile: (%v)", err) |
| 84 | + } |
| 85 | + log.Printf("renamed Dockerfile to %s and replaced with newer version", newDockerfilePath) |
| 86 | + log.Print("Compare the new Dockerfile to your old one and manually migrate any customizations") |
| 87 | + |
| 88 | + s := &scaffold.Scaffold{} |
| 89 | + err = s.Execute(cfg, |
| 90 | + &ansible.Main{}, |
| 91 | + &ansible.GopkgToml{}, |
| 92 | + &dockerfile, |
| 93 | + &ansible.Entrypoint{}, |
| 94 | + &ansible.UserSetup{}, |
| 95 | + ) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("add scaffold failed: (%v)", err) |
| 98 | + } |
| 99 | +} |
0 commit comments