Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Feature ansible modules (#55)
Browse files Browse the repository at this point in the history
Add ansible modules to control rancher

* Add dryRun flag for all write operation
* Add ansible module cattlectl_apply
* Add ansible module cattlectl_list
* Add ansible module cattlectl_delete
* Document ansible modules
* Update changelog
  • Loading branch information
wtschreiter authored May 24, 2020
1 parent 927a7cc commit 84a6513
Show file tree
Hide file tree
Showing 72 changed files with 1,492 additions and 461 deletions.
20 changes: 16 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ env:
install: true
script:
- go test -v -mod=vendor ./...
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -d -s -w -extldflags \"-static\"" -a -tags netgo -installsuffix netgo -mod=vendor -o build/linux/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz build/linux/
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -s -w" -a -tags netgo -installsuffix netgo -mod=vendor -o build/darwin/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz build/darwin/

- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -d -s -w -extldflags \"-static\"" -a -tags netgo -installsuffix netgo -mod=vendor -o build/cli/linux/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz build/cli/linux/

- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -s -w" -a -tags netgo -installsuffix netgo -mod=vendor -o build/cli/darwin/cattlectl
- tar czpvf build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz build/cli/darwin/

- mkdir -p build/ansible/linux
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -d -s -w -extldflags \"-static\"" -a -tags netgo -installsuffix netgo -mod=vendor -o build/ansible/linux ./ansible/...
- tar czpvf build/cattlectl-ansible-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz build/ansible/linux/

- mkdir -p build/ansible/darwin
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=${TRAVIS_TAG:-0.0.0-dev} -s -w" -a -tags netgo -installsuffix netgo -mod=vendor -o build/ansible/darwin ./ansible/...
- tar czpvf build/cattlectl-ansible-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz build/ansible/darwin/
deploy:
provider: releases
api_key: $GITHUB_API_KEY
file:
- build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz
- build/cattlectl-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz
- build/cattlectl-ansible-${TRAVIS_TAG:-0.0.0-dev}-linux.tar.gz
- build/cattlectl-ansible-${TRAVIS_TAG:-0.0.0-dev}-darwin.tar.gz
skip_cleanup: true
on:
repo: bitgrip/cattlectl
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.

### Added

* Add Ansible binary modules
* cattlectl_apply
* cattlectl_list
* cattlectl_delete
* (#48) Add support for multiple YAML objects in a single file
* Each not empty object must have fields
* `api_version`
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ bitgrip/cattlectl apply
Build from source
-----------------

### cattlectl

```bash
go install \
-ldflags "-X github.com/bitgrip/cattlectl/internal/pkg/ctl.Version=$(git describe --tags) -s -w" \
-a -tags netgo -installsuffix netgo -mod=vendor
```

### Ansible modules

```
go build -mod=vendor -o ~/.ansible/plugins/modules/ ./ansible/...
```

Docs
----

Expand Down
92 changes: 92 additions & 0 deletions ansible/cattlectl_apply/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright © 2020 Bitgrip <[email protected]>
//
// 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.

package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/bitgrip/cattlectl/ansible/utils"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/descriptor"
"github.com/bitgrip/cattlectl/internal/pkg/template"
)

type moduleArgs struct {
ApplyFile string `json:"file"`
ValueFiles []string `json:"value_files"`
Values map[string]interface{} `json:"values"`
WorkingDirectory string `json:"working_directory"`
utils.AccessArgs `json:",inline"`
}

type listResponse struct {
ApplyResult descriptor.ConvergeResult `json:"apply_result"`
utils.BaseResponse `json:",inline"`
}

func main() {
var moduleArgs moduleArgs
utils.ReadArguments(&moduleArgs)

var response listResponse
response.Version = ctl.Version

if moduleArgs.WorkingDirectory != "" {
err := os.Chdir(moduleArgs.WorkingDirectory)
if err != nil {
response.Msg = fmt.Sprintf("Failed to apply file %s: - %v", moduleArgs.ApplyFile, err)
response.Failed = true
utils.FailJson(response)
}
}

values, err := utils.LoadValues(moduleArgs.Values, moduleArgs.ValueFiles...)
if err != nil {
response.Msg = fmt.Sprintf("Failed to apply file %s: - %v", moduleArgs.ApplyFile, err)
response.Failed = true
utils.FailJson(response)
}
fileContent, err := ioutil.ReadFile(moduleArgs.ApplyFile)
if err != nil {
response.Msg = fmt.Sprintf("Failed to apply file %s: - %v", moduleArgs.ApplyFile, err)
response.Failed = true
utils.FailJson(response)
}
projectData, err := template.BuildTemplate(fileContent, values, filepath.Dir(moduleArgs.ApplyFile), false)
if err != nil {
response.Msg = fmt.Sprintf("Failed to apply file %s: - %v", moduleArgs.ApplyFile, err)
response.Failed = true
utils.FailJson(response)
}

result, err := ctl.ApplyDescriptor(
moduleArgs.ApplyFile,
projectData,
map[string]interface{}{},
utils.BuildRancherConfig(moduleArgs.AccessArgs),
)

if err != nil {
response.Msg = "Failed to apply descriptor: " + err.Error()
response.Failed = true
utils.FailJson(response)
}
response.ApplyResult = result
response.Changed = len(result.CreatedResources) > 0 || len(result.UpgradedResources) > 0
utils.ExitJson(response)
}
63 changes: 63 additions & 0 deletions ansible/cattlectl_delete/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright © 2020 Bitgrip <[email protected]>
//
// 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.

package main

import (
"fmt"

"github.com/bitgrip/cattlectl/ansible/utils"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
)

type moduleArgs struct {
ProjectName string `json:"project_name"`
Namespace string `json:"namespace"`
Kind string `json:"kind"`
Names []string `json:"names"`
utils.AccessArgs `json:",inline"`
}

type listResponse struct {
Deleted []string `json:"deleted"`
utils.BaseResponse `json:",inline"`
}

func main() {
var moduleArgs moduleArgs
utils.ReadArguments(&moduleArgs)

var response listResponse
response.Version = ctl.Version
for _, name := range moduleArgs.Names {
deleted, err := ctl.DeleteProjectResouce(
moduleArgs.ProjectName,
moduleArgs.Namespace,
moduleArgs.Kind,
name,
utils.BuildRancherConfig(moduleArgs.AccessArgs),
)
if err != nil {
response.Msg = fmt.Sprintf("Failed to delete %s %s: - %v", moduleArgs.Kind, name, err)
response.Failed = true
utils.FailJson(response)
}
if deleted {
response.Deleted = append(response.Deleted, name)
response.Changed = true
}
}
response.Msg = fmt.Sprintf("Deleted %v %ss", len(response.Deleted), moduleArgs.Kind)
utils.ExitJson(response)
}
59 changes: 59 additions & 0 deletions ansible/cattlectl_list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 2020 Bitgrip <[email protected]>
//
// 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.

package main

import (
"fmt"

"github.com/bitgrip/cattlectl/ansible/utils"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
)

type moduleArgs struct {
ProjectName string `json:"project_name"`
Namespace string `json:"namespace"`
Kind string `json:"kind"`
Pattern string `json:"pattern"`
utils.AccessArgs `json:",inline"`
}

type listResponse struct {
Matches []string `json:"matches"`
utils.BaseResponse `json:",inline"`
}

func main() {
var moduleArgs moduleArgs
utils.ReadArguments(&moduleArgs)

matches, err := ctl.ListProjectResouces(
moduleArgs.ProjectName,
moduleArgs.Namespace,
moduleArgs.Kind,
moduleArgs.Pattern,
utils.BuildRancherConfig(moduleArgs.AccessArgs),
)

var response listResponse
response.Version = ctl.Version
if err != nil {
response.Msg = "Failed to list resources: " + err.Error()
response.Failed = true
utils.FailJson(response)
}
response.Msg = fmt.Sprintf("List %v matches", len(matches))
response.Matches = matches
utils.ExitJson(response)
}
Loading

0 comments on commit 84a6513

Please sign in to comment.