Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add location library to elastic-package #303

Merged
merged 18 commits into from
Apr 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/cleanup/service_logs.go
Original file line number Diff line number Diff line change
@@ -7,24 +7,24 @@ package cleanup
import (
"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/logger"
)

// ServiceLogs function removes service logs from temporary directory in the `~/.elastic-package`.
func ServiceLogs() (string, error) {
logger.Debug("Clean all service logs")

serviceLogsDir, err := install.ServiceLogsDir()
locationManager, err := locations.NewLocationManager()
if err != nil {
return "", errors.Wrap(err, "can't find service logs dir")
}

logger.Debugf("Remove folder content (path: %s)", serviceLogsDir)
err = files.RemoveContent(serviceLogsDir)
logger.Debugf("Remove folder content (path: %s)", locationManager.ServiceLogDir())
err = files.RemoveContent(locationManager.ServiceLogDir())
if err != nil {
return "", errors.Wrapf(err, "can't remove content (path: %s)", serviceLogsDir)
return "", errors.Wrapf(err, "can't remove content (path: %s)", locationManager.ServiceLogDir())
}
return serviceLogsDir, nil
return locationManager.ServiceLogDir(), nil
}
6 changes: 3 additions & 3 deletions internal/cleanup/stack.go
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@ import (

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/configuration/locations"
"github.com/elastic/elastic-package/internal/packages"

"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/logger"
)

@@ -30,11 +30,11 @@ func Stack() (string, error) {
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot)
}

stackPackagesDir, err := install.StackPackagesDir()
locationManager, err := locations.NewLocationManager()
if err != nil {
return "", errors.Wrap(err, "can't find stack packages dir")
}
destinationDir := filepath.Join(stackPackagesDir, m.Name)
destinationDir := filepath.Join(locationManager.PackagesDir(), m.Name)

_, err = os.Stat(destinationDir)
if err != nil && !os.IsNotExist(err) {
106 changes: 106 additions & 0 deletions internal/configuration/locations/locations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// Package locations manages base file and directory locations from within the elastic-package config
package locations

import (
"os"
"path/filepath"

"github.com/pkg/errors"
)

const (
elasticPackageDir = ".elastic-package"
stackDir = "stack"
packagesDir = "development"

temporaryDir = "tmp"
deployerDir = "deployer"

kubernetesDeployerElasticAgentYmlFile = "elastic-agent.yml"
terraformDeployerYmlFile = "terraform-deployer.yml"
)

var (
serviceLogsDir = filepath.Join(temporaryDir, "service_logs")
kubernetesDeployerDir = filepath.Join(deployerDir, "kubernetes")
terraformDeployerDir = filepath.Join(deployerDir, "terraform")
)

//LocationManager maintains an instance of a config path location
type LocationManager struct {
stackPath string
}

// NewLocationManager returns a new manager to track the Configuration dir
func NewLocationManager() (*LocationManager, error) {
cfg, err := configurationDir()
if err != nil {
return nil, errors.Wrap(err, "error getting config dir")
}

return &LocationManager{stackPath: cfg}, nil

}

// RootDir returns the root elastic-package dir
func (loc LocationManager) RootDir() string {
return loc.stackPath
}

// TempDir returns the temp directory location
func (loc LocationManager) TempDir() string {
return filepath.Join(loc.stackPath, temporaryDir)
}

// DeployerDir returns the deployer directory location
func (loc LocationManager) DeployerDir() string {
return filepath.Join(loc.stackPath, deployerDir)
}

// StackDir returns the stack directory location
func (loc LocationManager) StackDir() string {
return filepath.Join(loc.stackPath, stackDir)
}

// PackagesDir returns the packages directory location
func (loc LocationManager) PackagesDir() string {
return filepath.Join(loc.stackPath, stackDir, packagesDir)
}

// KubernetesDeployerDir returns the Kubernetes Deployer directory location
func (loc LocationManager) KubernetesDeployerDir() string {
return filepath.Join(loc.stackPath, kubernetesDeployerDir)
}

// KubernetesDeployerAgentYml returns the Kubernetes Deployer Elastic Agent yml
func (loc LocationManager) KubernetesDeployerAgentYml() string {
return filepath.Join(loc.stackPath, kubernetesDeployerDir, kubernetesDeployerElasticAgentYmlFile)
}

// TerraformDeployerDir returns the Terraform Directory
func (loc LocationManager) TerraformDeployerDir() string {
return filepath.Join(loc.stackPath, terraformDeployerDir)
}

// TerraformDeployerYml returns the Terraform deployer yml file
func (loc LocationManager) TerraformDeployerYml() string {
return filepath.Join(loc.stackPath, terraformDeployerDir, terraformDeployerYmlFile)
}

// ServiceLogDir returns the log directory
func (loc LocationManager) ServiceLogDir() string {
return filepath.Join(loc.stackPath, serviceLogsDir)
}

// configurationDir returns the configuration directory location
func configurationDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "reading home dir failed")
}
return filepath.Join(homeDir, elasticPackageDir), nil
}
6 changes: 4 additions & 2 deletions internal/install/application_configuration.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ import (

"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"github.com/elastic/elastic-package/internal/configuration/locations"
)

// ApplicationConfiguration represents the configuration of the elastic-package.
@@ -66,12 +68,12 @@ func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs {

// Configuration function returns the elastic-package configuration.
func Configuration() (*ApplicationConfiguration, error) {
configPath, err := configurationDir()
configPath, err := locations.NewLocationManager()
if err != nil {
return nil, errors.Wrap(err, "can't read configuration directory")
}

cfg, err := ioutil.ReadFile(filepath.Join(configPath, applicationConfigurationYmlFile))
cfg, err := ioutil.ReadFile(filepath.Join(configPath.RootDir(), applicationConfigurationYmlFile))
if err != nil {
return nil, errors.Wrap(err, "can't read configuration file")
}
128 changes: 29 additions & 99 deletions internal/install/install.go
Original file line number Diff line number Diff line change
@@ -12,30 +12,15 @@ import (
"strings"

"github.com/pkg/errors"
)

const (
elasticPackageDir = ".elastic-package"
stackDir = "stack"
packagesDir = "development"
temporaryDir = "tmp"
deployerDir = "deployer"

kubernetesDeployerElasticAgentYmlFile = "elastic-agent.yml"
terraformDeployerYmlFile = "terraform-deployer.yml"
)

var (
serviceLogsDir = filepath.Join(temporaryDir, "service_logs")
kubernetesDeployerDir = filepath.Join(deployerDir, "kubernetes")
terraformDeployerDir = filepath.Join(deployerDir, "terraform")
"github.com/elastic/elastic-package/internal/configuration/locations"
)

const versionFilename = "version"

// EnsureInstalled method installs once static resources for the testing Docker stack.
func EnsureInstalled() error {
elasticPackagePath, err := configurationDir()
elasticPackagePath, err := locations.NewLocationManager()
if err != nil {
return errors.Wrap(err, "failed locating the configuration directory")
}
@@ -83,116 +68,61 @@ func EnsureInstalled() error {
return nil
}

// StackDir method returns the stack directory (see: stackDir).
func StackDir() (string, error) {
configurationDir, err := configurationDir()
if err != nil {
return "", errors.Wrap(err, "locating configuration directory failed")
}
return filepath.Join(configurationDir, stackDir), nil
}

// StackPackagesDir method returns the stack packages directory used for package development.
func StackPackagesDir() (string, error) {
stackDir, err := StackDir()
if err != nil {
return "", errors.Wrap(err, "locating stack directory failed")
}
return filepath.Join(stackDir, packagesDir), nil
}

// ServiceLogsDir function returns the location of the directory to store service logs on the
// local filesystem, i.e. the same one where elastic-package is installed.
func ServiceLogsDir() (string, error) {
configurationDir, err := configurationDir()
if err != nil {
return "", errors.Wrap(err, "locating configuration directory failed")
}
return filepath.Join(configurationDir, serviceLogsDir), nil
}

// TerraformDeployerComposeFile function returns the path to the Terraform service deployer's definitions.
func TerraformDeployerComposeFile() (string, error) {
configurationDir, err := configurationDir()
if err != nil {
return "", errors.Wrap(err, "locating configuration directory failed")
}
return filepath.Join(configurationDir, terraformDeployerDir, terraformDeployerYmlFile), nil
}

// KubernetesDeployerElasticAgentFile function returns the path to the Elastic Agent YAML definition for the Kubernetes cluster.
func KubernetesDeployerElasticAgentFile() (string, error) {
configurationDir, err := configurationDir()
if err != nil {
return "", errors.Wrap(err, "locating configuration directory failed")
}
return filepath.Join(configurationDir, kubernetesDeployerDir, kubernetesDeployerElasticAgentYmlFile), nil
}

func configurationDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "reading home dir failed")
}
return filepath.Join(homeDir, elasticPackageDir), nil
}

func checkIfAlreadyInstalled(elasticPackagePath string) (bool, error) {
_, err := os.Stat(elasticPackagePath)
func checkIfAlreadyInstalled(elasticPackagePath *locations.LocationManager) (bool, error) {
_, err := os.Stat(elasticPackagePath.StackDir())
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, errors.Wrapf(err, "stat file failed (path: %s)", elasticPackagePath)
}
return checkIfLatestVersionInstalled(elasticPackagePath)
return checkIfLatestVersionInstalled(elasticPackagePath.RootDir())
}

func createElasticPackageDirectory(elasticPackagePath string) error {
err := os.RemoveAll(elasticPackagePath) // remove in case of potential upgrade
func createElasticPackageDirectory(elasticPackagePath *locations.LocationManager) error {
err := os.RemoveAll(elasticPackagePath.RootDir()) // remove in case of potential upgrade
if err != nil {
return errors.Wrapf(err, "removing directory failed (path: %s)", elasticPackagePath)
}

err = os.MkdirAll(elasticPackagePath, 0755)
err = os.MkdirAll(elasticPackagePath.RootDir(), 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", elasticPackagePath)
}
return nil
}

func writeStackResources(elasticPackagePath string) error {
stackPath := filepath.Join(elasticPackagePath, stackDir)
packagesPath := filepath.Join(stackPath, packagesDir)
err := os.MkdirAll(packagesPath, 0755)
func writeStackResources(elasticPackagePath *locations.LocationManager) error {

err := os.MkdirAll(elasticPackagePath.PackagesDir(), 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", packagesPath)
return errors.Wrapf(err, "creating directory failed (path: %s)", elasticPackagePath.PackagesDir())
}

err = writeStaticResource(err, filepath.Join(stackPath, "kibana.config.yml"), kibanaConfigYml)
err = writeStaticResource(err, filepath.Join(stackPath, "healthcheck.sh"), kibanaHealthcheckSh)
err = writeStaticResource(err, filepath.Join(stackPath, "snapshot.yml"), snapshotYml)
err = writeStaticResource(err, filepath.Join(stackPath, "package-registry.config.yml"), packageRegistryConfigYml)
err = writeStaticResource(err, filepath.Join(stackPath, "Dockerfile.package-registry"), packageRegistryDockerfile)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.StackDir(), "kibana.config.yml"), kibanaConfigYml)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.StackDir(), "healthcheck.sh"), kibanaHealthcheckSh)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.StackDir(), "snapshot.yml"), snapshotYml)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.StackDir(), "package-registry.config.yml"), packageRegistryConfigYml)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.StackDir(), "Dockerfile.package-registry"), packageRegistryDockerfile)

if err != nil {
return errors.Wrap(err, "writing static resource failed")
}
return nil
}

func writeKubernetesDeployerResources(elasticPackagePath string) error {
kubernetesDeployer := filepath.Join(elasticPackagePath, kubernetesDeployerDir)
err := os.MkdirAll(kubernetesDeployer, 0755)
func writeKubernetesDeployerResources(elasticPackagePath *locations.LocationManager) error {
err := os.MkdirAll(elasticPackagePath.KubernetesDeployerDir(), 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", kubernetesDeployer)
return errors.Wrapf(err, "creating directory failed (path: %s)", elasticPackagePath.KubernetesDeployerDir())
}

appConfig, err := Configuration()
if err != nil {
return errors.Wrap(err, "can't read application configuration")
}

err = writeStaticResource(err, filepath.Join(kubernetesDeployer, kubernetesDeployerElasticAgentYmlFile),
err = writeStaticResource(err, elasticPackagePath.KubernetesDeployerAgentYml(),
strings.ReplaceAll(kubernetesDeployerElasticAgentYml, "{{ ELASTIC_AGENT_IMAGE_REF }}",
appConfig.DefaultStackImageRefs().ElasticAgent))
if err != nil {
@@ -201,14 +131,14 @@ func writeKubernetesDeployerResources(elasticPackagePath string) error {
return nil
}

func writeTerraformDeployerResources(elasticPackagePath string) error {
terraformDeployer := filepath.Join(elasticPackagePath, terraformDeployerDir)
func writeTerraformDeployerResources(elasticPackagePath *locations.LocationManager) error {
terraformDeployer := elasticPackagePath.TerraformDeployerDir()
err := os.MkdirAll(terraformDeployer, 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", terraformDeployer)
}

err = writeStaticResource(err, filepath.Join(terraformDeployer, terraformDeployerYmlFile), terraformDeployerYml)
err = writeStaticResource(err, elasticPackagePath.TerraformDeployerYml(), terraformDeployerYml)
err = writeStaticResource(err, filepath.Join(terraformDeployer, "Dockerfile"), terraformDeployerDockerfile)
err = writeStaticResource(err, filepath.Join(terraformDeployer, "run.sh"), terraformDeployerRun)
if err != nil {
@@ -217,9 +147,9 @@ func writeTerraformDeployerResources(elasticPackagePath string) error {
return nil
}

func writeConfigFile(elasticPackagePath string) error {
func writeConfigFile(elasticPackagePath *locations.LocationManager) error {
var err error
err = writeStaticResource(err, filepath.Join(elasticPackagePath, applicationConfigurationYmlFile), applicationConfigurationYml)
err = writeStaticResource(err, filepath.Join(elasticPackagePath.RootDir(), applicationConfigurationYmlFile), applicationConfigurationYml)
if err != nil {
return errors.Wrap(err, "writing static resource failed")
}
@@ -238,8 +168,8 @@ func writeStaticResource(err error, path, content string) error {
return nil
}

func createServiceLogsDir(elasticPackagePath string) error {
dirPath := filepath.Join(elasticPackagePath, serviceLogsDir)
func createServiceLogsDir(elasticPackagePath *locations.LocationManager) error {
dirPath := elasticPackagePath.ServiceLogDir()
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return errors.Wrapf(err, "mkdir failed (path: %s)", dirPath)
Loading