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

Commit

Permalink
add initial cleanup app
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jun 25, 2019
1 parent dbd329c commit 5b70945
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
60 changes: 31 additions & 29 deletions pkg/apps/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apps

import (
"github.com/moshloop/konfigadm/pkg/phases"
. "github.com/moshloop/konfigadm/pkg/types"
)

Expand All @@ -9,36 +10,37 @@ var Cleanup Phase = cleanup{}
type cleanup struct{}

func (c cleanup) ApplyPhase(sys *Config, ctx *SystemContext) ([]Command, Filesystem, error) {

// tdnf clean all
// /sbin/ldconfig
// /usr/sbin/pwconv
// /usr/sbin/grpconv
// rm /etc/resolv.conf
// ln -sf ../run/systemd/resolve/resolv.conf /etc/resolv.conf
// rm -rf /tmp/*
// rm -rf /usr/share/man/*
// rm -rf /usr/share/doc/*
// find /var/cache -type f -exec rm -rf {} \;
// unset HISTFILE
// echo -n > /root/.bash_history

// find /var/log -type f | while read -r f; do echo -ne '' > "$f"; done;
// echo -ne '' >/var/log/lastlog
// echo -ne '' >/var/log/wtmp
// echo -ne '' >/var/log/btmp

// echo -ne '' > /root/.bashrc
// echo -ne '' > /root/.bash_profile
// echo 'shopt -s histappend' >> /root/.bash_profile
// echo 'export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"' >> /root/.bash_profile

// rm -f /etc/ssh/{ssh_host_dsa_key,ssh_host_dsa_key.pub,ssh_host_ecdsa_key,ssh_host_ecdsa_key.pub,ssh_host_ed25519_key,ssh_host_ed25519_key.pub,ssh_host_rsa_key,ssh_host_rsa_key.pub}

fs := Filesystem{}
cmds := Commands{}

if sys.Cleanup == nil || !*sys.Cleanup {
return cmds.GetCommands(), fs, nil
}

for _, os := range phases.BaseOperatingSystems {
cmds.AddAll(os.GetPackageManager().CleanupCaches().GetCommands()...)
}

cmds.Add("rm -rf /tmp/* || true").
Add("rm -rf /var/log/cloud-init* || true").
Add("rm -rf /usr/share/man/* || true").
Add("rm -rf /usr/share/doc/* || true").
Add("rm -f /etc/ssh/{ssh_host_dsa_key,ssh_host_dsa_key.pub,ssh_host_ecdsa_key,ssh_host_ecdsa_key.pub,ssh_host_ed25519_key,ssh_host_ed25519_key.pub,ssh_host_rsa_key,ssh_host_rsa_key.pub} || true").
Add("find /var/cache -type f -exec rm -rf {} \\;").
Add("find /var/log -type f | while read -r f; do echo -ne '' > \"$f\"; done;").
Add("cloud-init clean || true")

fs["/root/.bash_profile"] = File{Content: "shopt -s histappend"}
fs["/etc/machine-id"] = File{Content: ""}
fs["/root/.bash_history"] = File{Content: ""}
fs["/root/.bashrc"] = File{Content: ""}

// TODO
// dd if=/dev/zero of=/EMPTY bs=1M 2>/dev/null || echo "dd exit code $? is suppressed"
// rm -f /EMPTY
//redhat
//sed -i '/^\(HWADDR\|UUID\)=/d' /etc/sysconfig/network-scripts/ifcfg-*
//debian /etc/udev/rules.d/70-persistent-net.rules

// > /etc/machine-id

return []Command{}, Filesystem{}, nil
return cmds.GetCommands(), fs, nil
}
1 change: 1 addition & 0 deletions pkg/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
phases.Services,
phases.Files,
phases.CommandsPhase,
apps.Cleanup,
}
})
}
11 changes: 6 additions & 5 deletions pkg/phases/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type AptPackageManager struct {
}

func (p AptPackageManager) Install(pkg ...string) Commands {
return NewCommand("DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends " + strings.Join(utils.ReplaceAllInSlice(pkg, "==", "="), " "))
return NewCommand("DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-downgrades --no-install-recommends " + strings.Join(utils.ReplaceAllInSlice(pkg, "==", "="), " "))
}

func (p AptPackageManager) Uninstall(pkg ...string) Commands {
Expand All @@ -24,7 +24,7 @@ func (p AptPackageManager) Uninstall(pkg ...string) Commands {

func (p AptPackageManager) Mark(pkg ...string) Commands {
cmds := NewCommand("apt-get mark -y " + strings.Join(utils.SplitAllInSlice(pkg, "=", 0), " "))
return cmds.AddDependency("apt-get install -y apt-mark")
return *cmds.AddDependency("apt-get install -y apt-mark")
}

func (p AptPackageManager) ListInstalled() string {
Expand All @@ -45,6 +45,7 @@ func (p AptPackageManager) GetInstalledVersion(pkg string) string {
status := strings.Split(stdout, "\t")[0]
version := strings.Split(stdout, "\t")[1]

log.Tracef("package: %s, status: %s, version: %s", pkg, status, version)
if status != "installed" {
log.Debugf("%s is in db, but is not installed: %s", pkg, status)
return ""
Expand All @@ -53,7 +54,7 @@ func (p AptPackageManager) GetInstalledVersion(pkg string) string {
}

func (p AptPackageManager) AddRepo(uri string, channel string, versionCodeName string, name string, gpgKey string) Commands {
cmds := Commands{}
cmds := &Commands{}
if channel == "" {
channel = "main"
}
Expand Down Expand Up @@ -81,10 +82,10 @@ func (p AptPackageManager) AddRepo(uri string, channel string, versionCodeName s
cmds = cmds.Add(fmt.Sprintf("curl -skL \"%s\" | apt-key add -", gpgKey))
}

return cmds.Add(fmt.Sprintf("echo deb [arch=amd64] %s %s %s > /etc/apt/sources.list.d/%s.list", uri, versionCodeName, channel, name))
return *cmds.Add(fmt.Sprintf("echo deb [arch=amd64] %s %s %s > /etc/apt/sources.list.d/%s.list", uri, versionCodeName, channel, name))
}

func (p AptPackageManager) CleanupCaches() Commands {
set := Commands{}
return set.Add("apt-get -y autoremove --purge", "apt-get -y clean", "apt-get -y autoclean")
return *set.Add("apt-get -y autoremove --purge", "apt-get -y clean", "apt-get -y autoclean")
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ type Config struct {
Extra *cloudinit.CloudInit `yaml:"extra,omitempty"`
Services map[string]Service `yaml:"services,omitempty"`
Users []User `yaml:"users,omitempty"`
Cleanup *bool `yaml:"cleanup,omitempty"`
Context *SystemContext `yaml:"-"`
}

Expand Down

0 comments on commit 5b70945

Please sign in to comment.