Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Updated directory structure and have versioned names
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetguptanitj committed Aug 8, 2018
1 parent b2dce72 commit 397795f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 54 deletions.
6 changes: 2 additions & 4 deletions apis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
type EtcdAdmConfig struct {
Version string
ReleaseURL string
InstallBaseDir string
CertificatesDir string

DownloadConnectTimeout time.Duration
Expand Down Expand Up @@ -139,11 +138,10 @@ func setDynamicDefaults(cfg *EtcdAdmConfig) error {
cfg.Name = name
}

cfg.InstallDir = filepath.Join(cfg.InstallBaseDir, fmt.Sprintf("etcd-v%s", cfg.Version))
cfg.CacheDir = filepath.Join(constants.DefaultCacheBaseDir, fmt.Sprintf("etcd-v%s", cfg.Version))
cfg.CacheDir = filepath.Join(constants.DefaultCacheBaseDir, "etcd", fmt.Sprintf("v%s", cfg.Version))
cfg.EtcdExecutable = filepath.Join(cfg.InstallDir, "etcd")
cfg.EtcdctlExecutable = filepath.Join(cfg.InstallDir, "etcdctl")
cfg.EtcdctlShellWrapper = filepath.Join(cfg.InstallBaseDir, "etcdctl.sh")
cfg.EtcdctlShellWrapper = filepath.Join(cfg.InstallDir, "etcdctl.sh")

cfg.GOMAXPROCS = runtime.NumCPU()

Expand Down
67 changes: 27 additions & 40 deletions binary/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binary

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -113,61 +114,47 @@ func downloadURL(releaseURL, version string) string {
}

// InstallFromCache method installs the binaries from cache directory
func InstallFromCache(version, installBaseDir, installDir, cacheDir string) (bool, error) {
func InstallFromCache(version, installDir, cacheDir string) (bool, error) {
archive := filepath.Join(cacheDir, releaseFile(version))
if _, err := os.Stat(archive); os.IsNotExist(err) {
return false, nil
}
// Remove installDir if already present
if err := os.RemoveAll(installDir); err != nil {
return true, fmt.Errorf("unable to clean install directory: %s", err)
}
// Create installDir
if err := os.MkdirAll(installDir, 0755); err != nil {
return true, fmt.Errorf("unable to create install directory: %s", err)
// Create a tmp dir
tmpDir, err := ioutil.TempDir("", "etcd")
if err != nil {
return true, fmt.Errorf("unable to create tmp dir %s to extract etcd archive", err)
}
// Extract tar to installDir
if err := extract(installDir, archive); err != nil {
defer os.RemoveAll(tmpDir)
// Extract tar to tmp location
if err := extract(tmpDir, archive); err != nil {
return true, fmt.Errorf("unable to extract etcd archive: %s", err)
}
// Create symlinks
if err := createSymLinks(installDir, installBaseDir); err != nil {
return false, fmt.Errorf("unable to create symlinks: %s", err)
// Copy binaries
if err := Install(tmpDir, installDir); err != nil {
return false, fmt.Errorf("unable to copy binaries: %s", err)
}
return true, nil
}

// Uninstall removes installed binaries and symlinks
func Uninstall(version, installBaseDir, installDir string) error {
// Remove binaries
if err := os.RemoveAll(installDir); err != nil {
return fmt.Errorf("unable to remove install directory: %s", err)
}
// Remove symlinks
if err := deleteSymLinks(installBaseDir); err != nil {
return fmt.Errorf("unable to remove symlinks: %s", err)
}
return nil
}

func createSymLinks(installDir, symLinkDir string) error {
etcdBinaryPath := filepath.Join(installDir, "etcd")
etcdSymLinkPath := filepath.Join(symLinkDir, "etcd")
etcdctlBinaryPath := filepath.Join(installDir, "etcdctl")
etcdctlSymLinkPath := filepath.Join(symLinkDir, "etcdctl")

if err := os.Symlink(etcdBinaryPath, etcdSymLinkPath); err != nil {
//Install copies binaries from srcDir to installDir
func Install(srcDir, installDir string) error {
etcdSrcPath := filepath.Join(srcDir, "etcd")
etcdDestPath := filepath.Join(installDir, "etcd")
etcdctlSrcPath := filepath.Join(srcDir, "etcdctl")
etcdctlDestPath := filepath.Join(installDir, "etcdctl")
if err := util.CopyFile(etcdSrcPath, etcdDestPath); err != nil {
return err
}
return os.Symlink(etcdctlBinaryPath, etcdctlSymLinkPath)
return util.CopyFile(etcdctlSrcPath, etcdctlDestPath)
}

// deleteSymLinks deletes symlinks created for etcd binaires
func deleteSymLinks(symLinkDir string) error {
etcdSymLinkPath := filepath.Join(symLinkDir, "etcd")
etcdctlSymLinkPath := filepath.Join(symLinkDir, "etcdctl")
if err := os.Remove(etcdSymLinkPath); err != nil {
// Uninstall removes installed binaries and symlinks
func Uninstall(version, installDir string) error {
// Remove binaries
etcdPath := filepath.Join(installDir, "etcd")
etcdctlPath := filepath.Join(installDir, "etcdctl")
if err := os.Remove(etcdPath); err != nil {
return err
}
return os.Remove(etcdctlSymLinkPath)
return os.Remove(etcdctlPath)
}
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var initCmd = &cobra.Command{
log.Fatalf("[defaults] Error: %s", err)
}
// etcd binaries installation
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallBaseDir, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
if err != nil {
log.Fatalf("[install] Artifact could not be installed from cache: %s", err)
}
Expand All @@ -33,7 +33,7 @@ var initCmd = &cobra.Command{
log.Fatalf("[install] Unable to fetch artifact from upstream: %s", err)
}
// Try installing binaries from cache now
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallBaseDir, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
if err != nil {
log.Fatalf("[install] Artifact could not be installed from cache: %s", err)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func init() {
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.Version, "version", constants.DefaultVersion, "etcd version")
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.ReleaseURL, "release-url", constants.DefaultReleaseURL, "URL used to download etcd")
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.CertificatesDir, "certs-dir", constants.DefaultCertificateDir, "certificates directory")
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallBaseDir, "install-base-dir", constants.DefaultInstallBaseDir, "install base directory")
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallDir, "install-dir", constants.DefaultInstallDir, "install directory")
initCmd.PersistentFlags().StringVar(&etcdAdmConfig.Snapshot, "snapshot", "", "Etcd v3 snapshot file used to initialize member")
initCmd.PersistentFlags().BoolVar(&etcdAdmConfig.SkipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
initCmd.PersistentFlags().DurationVar(&etcdAdmConfig.DownloadConnectTimeout, "download-connect-timeout", constants.DefaultDownloadConnectTimeout, "Maximum time in seconds that you allow the connection to the server to take.")
Expand Down
6 changes: 3 additions & 3 deletions cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var joinCmd = &cobra.Command{
log.Fatalf("[defaults] Error: %s", err)
}
// etcd binaries installation
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallBaseDir, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
if err != nil {
log.Fatalf("[install] Artifact could not be installed from cache: %s", err)
}
Expand All @@ -49,7 +49,7 @@ var joinCmd = &cobra.Command{
log.Fatalf("[install] Unable to fetch artifact from upstream: %s", err)
}
// Try installing binaries from cache now
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallBaseDir, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
inCache, err := binary.InstallFromCache(etcdAdmConfig.Version, etcdAdmConfig.InstallDir, etcdAdmConfig.CacheDir)
if err != nil {
log.Fatalf("[install] Artifact could not be installed from cache: %s", err)
}
Expand Down Expand Up @@ -116,5 +116,5 @@ func init() {
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.Version, "version", constants.DefaultVersion, "etcd version")
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.ReleaseURL, "release-url", constants.DefaultReleaseURL, "URL used to download etcd")
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.CertificatesDir, "certs-dir", constants.DefaultCertificateDir, "certificates directory")
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallBaseDir, "install-base-dir", constants.DefaultInstallBaseDir, "install base directory")
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallDir, "install-dir", constants.DefaultInstallDir, "install directory")
}
4 changes: 2 additions & 2 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var resetCmd = &cobra.Command{
log.Print(err)
}
// Remove binaries
if err := binary.Uninstall(etcdAdmConfig.Version, etcdAdmConfig.InstallBaseDir, etcdAdmConfig.InstallDir); err != nil {
if err := binary.Uninstall(etcdAdmConfig.Version, etcdAdmConfig.InstallDir); err != nil {
log.Printf("[binaries] Unable to uninstall binaries: %v", err)
}
if err = os.Remove(etcdAdmConfig.EtcdctlShellWrapper); err != nil {
Expand All @@ -67,6 +67,6 @@ var resetCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(resetCmd)
resetCmd.Flags().BoolVar(&skipRemoveMember, "skip-remove-member", constants.DefaultSkipRemoveMember, "Use skip-remove-member flag to skip the process of removing member from etcd cluster but clean everything else.")
resetCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallBaseDir, "install-base-dir", constants.DefaultInstallBaseDir, "install base directory")
resetCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallDir, "install-dir", constants.DefaultInstallDir, "install directory")
resetCmd.PersistentFlags().StringVar(&etcdAdmConfig.CertificatesDir, "certs-dir", constants.DefaultCertificateDir, "certificates directory")
}
4 changes: 2 additions & 2 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "time"

// Command-line flag defaults
const (
DefaultVersion = "3.3.8"
DefaultInstallBaseDir = "/opt/bin/"
DefaultVersion = "3.3.8"
DefaultInstallDir = "/opt/bin/"

DefaultReleaseURL = "https://github.com/coreos/etcd/releases/download"
DefaultBindAddressv4 = "0.0.0.0"
Expand Down
9 changes: 9 additions & 0 deletions util/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"os"
"os/exec"
)

// FileExists checks whether the file exists
Expand All @@ -14,3 +15,11 @@ func FileExists(path string) (bool, error) {
}
return true, nil
}

// Copy file from src to dest
func CopyFile(srcFile, destFile string) error {
if err := exec.Command("cp -f ", srcFile, destFile).Run(); err != nil {
return err
}
return nil
}

0 comments on commit 397795f

Please sign in to comment.