This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
72 changed files
with
1,492 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.