-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
481 additions
and
90 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
services/ansibler/server/ansible-playbooks/wireguard-uninstall.yml
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,37 @@ | ||
--- | ||
- hosts: all | ||
remote_user: root | ||
gather_facts: false | ||
become: yes | ||
|
||
pre_tasks: | ||
- name: Wait 300 seconds for target connection to become reachable/usable | ||
wait_for_connection: | ||
timeout: 300 | ||
|
||
# Gather facts manually after we made sure, VMs are accessible | ||
- name: Gather facts from nodes | ||
ansible.builtin.setup: | ||
|
||
tasks: | ||
- name: Bring down WireGuard interface | ||
ansible.builtin.command: wg-quick down wg0 | ||
ignore_errors: yes | ||
|
||
- name: Stop and disable systemd service for wg-quick@wg0 | ||
ansible.builtin.systemd: | ||
name: "wg-quick@wg0" | ||
enabled: no | ||
state: stopped | ||
|
||
- name: Delete WireGuard configuration directory | ||
ansible.builtin.file: | ||
path: "/etc/wireguard" | ||
state: absent | ||
|
||
- name: Remove WireGuard package via apt | ||
ansible.builtin.apt: | ||
pkg: | ||
- wireguard | ||
- wireguard-tools | ||
state: absent |
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
89 changes: 89 additions & 0 deletions
89
services/ansibler/server/domain/usecases/remove_utilities.go
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,89 @@ | ||
package usecases | ||
|
||
import ( | ||
"fmt" | ||
"github.com/rs/zerolog/log" | ||
"os" | ||
"path/filepath" | ||
|
||
cutils "github.com/berops/claudie/internal/utils" | ||
"github.com/berops/claudie/proto/pb" | ||
"github.com/berops/claudie/services/ansibler/server/utils" | ||
"github.com/berops/claudie/services/ansibler/templates" | ||
) | ||
|
||
func (u *Usecases) RemoveUtilities(req *pb.RemoveClaudieUtilitiesRequest) (*pb.RemoveClaudieUtilitiesResponse, error) { | ||
logger := cutils.CreateLoggerWithProjectAndClusterName(req.ProjectName, req.Current.ClusterInfo.Name) | ||
logger.Info().Msgf("Removing Claudie installed utilities") | ||
|
||
vpnInfo := &VPNInfo{ | ||
ClusterNetwork: req.Current.Network, | ||
NodepoolsInfos: []*NodepoolsInfo{ | ||
{ | ||
Nodepools: utils.NodePools{ | ||
Dynamic: cutils.GetCommonDynamicNodePools(req.Current.ClusterInfo.NodePools), | ||
Static: cutils.GetCommonStaticNodePools(req.Current.ClusterInfo.NodePools), | ||
}, | ||
PrivateKey: req.Current.ClusterInfo.PrivateKey, | ||
ClusterID: cutils.GetClusterID(req.Current.ClusterInfo), | ||
ClusterNetwork: req.Current.Network, | ||
}, | ||
}, | ||
} | ||
|
||
for _, lbCluster := range req.CurrentLbs { | ||
vpnInfo.NodepoolsInfos = append(vpnInfo.NodepoolsInfos, &NodepoolsInfo{ | ||
Nodepools: utils.NodePools{ | ||
Dynamic: cutils.GetCommonDynamicNodePools(lbCluster.ClusterInfo.NodePools), | ||
Static: cutils.GetCommonStaticNodePools(lbCluster.ClusterInfo.NodePools), | ||
}, | ||
PrivateKey: lbCluster.ClusterInfo.PrivateKey, | ||
ClusterID: cutils.GetClusterID(lbCluster.ClusterInfo), | ||
ClusterNetwork: req.Current.Network, | ||
}) | ||
} | ||
|
||
if err := removeWireguard(cutils.GetClusterID(req.Current.ClusterInfo), vpnInfo, u.SpawnProcessLimit); err != nil { | ||
return nil, fmt.Errorf("failed to remove wiregaurd from nodes: %w", err) | ||
} | ||
|
||
return &pb.RemoveClaudieUtilitiesResponse{Current: req.Current, CurrentLbs: req.CurrentLbs}, nil | ||
} | ||
|
||
func removeWireguard(clusterID string, vpnInfo *VPNInfo, spawnProcessLimit chan struct{}) error { | ||
clusterDirectory := filepath.Join(baseDirectory, outputDirectory, fmt.Sprintf("%s-%s", clusterID, cutils.CreateHash(cutils.HashLength))) | ||
if err := cutils.CreateDirectory(clusterDirectory); err != nil { | ||
return fmt.Errorf("failed to create directory %s: %w", clusterDirectory, err) | ||
} | ||
|
||
err := utils.GenerateInventoryFile(templates.AllNodesInventoryTemplate, clusterDirectory, AllNodesInventoryData{ | ||
NodepoolsInfo: vpnInfo.NodepoolsInfos, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error while creating inventory file for %s: %w", clusterDirectory, err) | ||
} | ||
|
||
for _, nodepoolInfo := range vpnInfo.NodepoolsInfos { | ||
if err := cutils.CreateKeyFile(nodepoolInfo.PrivateKey, clusterDirectory, fmt.Sprintf("%s.%s", nodepoolInfo.ClusterID, sshPrivateKeyFileExtension)); err != nil { | ||
return fmt.Errorf("failed to create key file for %s : %w", nodepoolInfo.ClusterID, err) | ||
} | ||
|
||
if err := cutils.CreateKeysForStaticNodepools(nodepoolInfo.Nodepools.Static, clusterDirectory); err != nil { | ||
return fmt.Errorf("failed to create key file(s) for static nodes : %w", err) | ||
} | ||
} | ||
|
||
ansible := utils.Ansible{ | ||
Playbook: wireguardUninstall, | ||
Inventory: utils.InventoryFileName, | ||
Directory: clusterDirectory, | ||
SpawnProcessLimit: spawnProcessLimit, | ||
} | ||
|
||
// Subsequent calling may fail, thus simply log the error. | ||
if err := ansible.RunAnsiblePlaybook(fmt.Sprintf("Remove Utils - %s", clusterID)); err != nil { | ||
log.Warn().Msgf("error while uninstalling wireguard ansible for %s : %w", clusterID, err) | ||
} | ||
|
||
return os.RemoveAll(clusterDirectory) | ||
} |
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