ansible

package
v0.2.4-r00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = &cobra.Command{
	Use:     "create [path to template]",
	Aliases: []string{"crt", "c"},
	Short:   "Create Ansible Run",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		if _, err := os.Stat(args[0]); os.IsNotExist(err) {
			return errors.New("Template doesn't exist at path " + args[0])
		}

		var format string
		{
			pathSlice := strings.Split(args[0], ".")
			format = pathSlice[len(pathSlice)-1]
		}

		template, err := os.ReadFile(args[0])
		if err != nil {
			fmt.Println("Error reading template file")
			return err
		}

		switch format {
		case "json":
		case "yml", "yaml":
			template, err = yaml.YAMLToJSON(template)
		default:
			return errors.New("Unsupported template format " + format)
		}

		run := &pb.Run{}
		err = json.Unmarshal(template, &run)
		if err != nil {
			fmt.Println("Error while parsing template")
			return err
		}

		ctx, client := MakeAnsibleServiceCleintOrFail()

		res, err := client.Create(ctx, &pb.CreateRunRequest{Run: run})
		if err != nil {
			return err
		}

		output, err := json.MarshalIndent(res, "-", " ")
		if err != nil {
			fmt.Println(res)
			return err
		}

		fmt.Println("Result: ", string(output))

		return nil
	},
}

createCmd represents the create command

View Source
var DeleteCmd = &cobra.Command{
	Use:     "delete [uuid]",
	Aliases: []string{"del", "remove", "d"},
	Short:   "Delete Ansible Run",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		uuid := args[0]
		if uuid == "" {
			return errors.New("empty uuid")
		}

		ctx, client := MakeAnsibleServiceCleintOrFail()

		res, err := client.Delete(ctx, &pb.DeleteRunRequest{
			Uuid: uuid,
		})
		if err != nil {
			return err
		}

		output, err := json.MarshalIndent(res, "-", " ")
		if err != nil {
			fmt.Println(res)
			return err
		}

		fmt.Println("Result: ", string(output))

		return nil
	},
}
View Source
var ExecCmd = &cobra.Command{
	Use:     "exec [service_id]",
	Aliases: []string{},
	Short:   "Exec Run",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx, client := MakeAnsibleServiceCleintOrFail()

		req := pb.ExecRunRequest{Uuid: args[0]}

		resp, err := client.Exec(ctx, &req)
		if err != nil {
			log.Fatal(err)
			return err
		}

		fmt.Println(resp)
		return nil
	},
}
View Source
var GetCmd = &cobra.Command{
	Use:     "get [uuid]",
	Aliases: []string{"get", "fetch"},
	Short:   "Get Ansible Run",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		uuid := args[0]
		if uuid == "" {
			return errors.New("empty uuid")
		}

		ctx, client := MakeAnsibleServiceCleintOrFail()

		res, err := client.Get(ctx, &pb.GetRunRequest{
			Uuid: uuid,
		})
		if err != nil {
			return err
		}

		output, err := json.MarshalIndent(res, "-", " ")
		if err != nil {
			fmt.Println(res)
			return err
		}

		fmt.Println("Result: ", string(output))

		return nil
	},
}
View Source
var ListCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"ls", "lst"},
	Short:   "List Ansible Runs",
	Args:    cobra.ExactArgs(0),
	RunE: func(cmd *cobra.Command, args []string) (err error) {

		ctx, client := MakeAnsibleServiceCleintOrFail()

		res, err := client.List(ctx, &pb.ListRunsRequest{})
		if err != nil {
			return err
		}

		output, err := json.MarshalIndent(res, "-", " ")
		if err != nil {
			fmt.Println(res)
			return err
		}

		fmt.Println("Runs: ", string(output))

		return nil
	},
}
View Source
var WatchCmd = &cobra.Command{
	Use:     "watch [service_id]",
	Aliases: []string{},
	Short:   "Watch Run",
	Args:    cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx, client := MakeAnsibleServiceCleintOrFail()

		req := pb.WatchRunRequest{Uuid: args[0]}

		resp, err := client.Watch(ctx, &req)
		if err != nil {
			log.Fatal(err)
			return err
		}
		whitespace := regexp.MustCompile(`^[\s]*$`)

		for {

			respObject, err := resp.Recv()

			if err == io.EOF {
				fmt.Println("All done")
				return nil
			}
			if err != nil {
				return err
			}

			ok, err := tools.PrintJsonDataQ(cmd, respObject)
			if err != nil {
				return err
			}

			if !ok {
				data := map[string]interface{}{}

				if err := json.Unmarshal([]byte(respObject.Job), &data); err != nil {
					continue
				}

				if whitespace.MatchString(data["stdout"].(string)) {
					continue
				}

				fmt.Println(data["stdout"])
			}

		}

	},
}

Functions

func MakeAnsibleServiceCleintOrFail

func MakeAnsibleServiceCleintOrFail() (context.Context, pb.AnsibleServiceClient)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL