-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Agent] Enable post install hooks #17241
Changes from 6 commits
042d9c6
a32b3a7
0af63c8
1fe0bf4
e73d37c
453546e
ea370ff
4f30025
069e438
98437b8
ca18a46
cd80ffe
64a61d0
b7a06ea
4584bdd
f3a0ba7
aab3104
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// 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 transpiler | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// StepList is a container that allow the same tree to be executed on multiple defined Step. | ||
type StepList struct { | ||
Steps []Step | ||
} | ||
|
||
// Step is an execution step which needs to be run. | ||
type Step interface { | ||
Execute(rootDir string) error | ||
} | ||
|
||
// Execute executes a list of steps. | ||
func (r *StepList) Execute(rootDir string) error { | ||
var err error | ||
for _, step := range r.Steps { | ||
err = step.Execute(rootDir) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MarshalYAML marsharl a steps list to YAML. | ||
func (r *StepList) MarshalYAML() (interface{}, error) { | ||
doc := make([]map[string]Step, 0, len(r.Steps)) | ||
|
||
for _, step := range r.Steps { | ||
var name string | ||
switch step.(type) { | ||
case *DeleteFileStep: | ||
name = "delete_file" | ||
case *MoveFileStep: | ||
name = "move_file" | ||
|
||
default: | ||
return nil, fmt.Errorf("unknown rule of type %T", step) | ||
} | ||
|
||
subdoc := map[string]Step{ | ||
name: step, | ||
} | ||
|
||
doc = append(doc, subdoc) | ||
} | ||
return doc, nil | ||
} | ||
|
||
// UnmarshalYAML unmarshal a YAML document into a RuleList. | ||
func (r *StepList) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var unpackTo []map[string]interface{} | ||
|
||
err := unmarshal(&unpackTo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// NOTE(ph): this is a bit of a hack because I want to make sure | ||
// the unpack strategy stay in the struct implementation and yaml | ||
// doesn't have a RawMessage similar to the JSON package, so partial unpack | ||
// is not possible. | ||
unpack := func(in interface{}, out interface{}) error { | ||
b, err := yaml.Marshal(in) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(b)) | ||
return errors.Wrap(yaml.Unmarshal(b, out), "ajaj") | ||
} | ||
|
||
var steps []Step | ||
|
||
for _, m := range unpackTo { | ||
ks := keys(m) | ||
if len(ks) > 1 { | ||
return fmt.Errorf("unknown rule identifier, expecting one identifier and received %d", len(ks)) | ||
} | ||
|
||
name := ks[0] | ||
fields := m[name] | ||
|
||
var s Step | ||
switch name { | ||
case "delete_file": | ||
s = &DeleteFileStep{} | ||
case "move_file": | ||
s = &MoveFileStep{} | ||
default: | ||
return fmt.Errorf("unknown rule of type %s", name) | ||
} | ||
|
||
if err := unpack(fields, s); err != nil { | ||
return err | ||
} | ||
|
||
steps = append(steps, s) | ||
} | ||
r.Steps = steps | ||
return nil | ||
} | ||
|
||
// DeleteFileStep removes a file from disk. | ||
type DeleteFileStep struct { | ||
Path string | ||
// FailOnMissing fails if file is already missing | ||
FailOnMissing bool | ||
} | ||
|
||
// Execute executes delete file step. | ||
func (r *DeleteFileStep) Execute(rootDir string) error { | ||
path := filepath.Join(rootDir, filepath.FromSlash(r.Path)) | ||
err := os.Remove(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to add some defensive programming here, even if we control the spec, we never know when this will not be the case. I think we should convert the I believe this is the case, we want to sandbox every command into a specific directly. I assume all our commands operate in the extracted directory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is something i was thinking about but did not want to sound too paraniod There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to be paranoid than sorry :) |
||
|
||
if os.IsNotExist(err) && r.FailOnMissing { | ||
// is not found and should be reported | ||
return err | ||
} | ||
|
||
if err != nil && !os.IsNotExist(err) { | ||
// report others | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteFile creates a DeleteFileStep | ||
func DeleteFile(path string, failOnMissing bool) *DeleteFileStep { | ||
return &DeleteFileStep{ | ||
Path: path, | ||
FailOnMissing: failOnMissing, | ||
} | ||
} | ||
|
||
// MoveFileStep moves a file to a new location. | ||
type MoveFileStep struct { | ||
Path string | ||
Target string | ||
// FailOnMissing fails if file is already missing | ||
FailOnMissing bool `yaml:"fail_on_missing" config:"fail_on_missing"` | ||
} | ||
|
||
// Execute executes move file step. | ||
func (r *MoveFileStep) Execute(rootDir string) error { | ||
path := filepath.Join(rootDir, filepath.FromSlash(r.Path)) | ||
target := filepath.Join(rootDir, filepath.FromSlash(r.Target)) | ||
|
||
err := os.Rename(path, target) | ||
|
||
if os.IsNotExist(err) && r.FailOnMissing { | ||
// is not found and should be reported | ||
return err | ||
} | ||
|
||
if err != nil && !os.IsNotExist(err) { | ||
// report others | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MoveFile creates a MoveFileStep | ||
func MoveFile(path, target string, failOnMissing bool) *MoveFileStep { | ||
return &MoveFileStep{ | ||
Path: path, | ||
Target: target, | ||
FailOnMissing: failOnMissing, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 hooks | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/elastic/beats/v7/x-pack/agent/pkg/agent/program" | ||
) | ||
|
||
type embeddedInstaller interface { | ||
Install(programName, version, installDir string) error | ||
} | ||
|
||
// Installer or zip packages | ||
type Installer struct { | ||
installer embeddedInstaller | ||
} | ||
|
||
// NewInstaller creates an installer able to install zip packages | ||
func NewInstaller(i embeddedInstaller) (*Installer, error) { | ||
return &Installer{ | ||
installer: i, | ||
}, nil | ||
} | ||
|
||
// Install performs installation of program in a specific version. | ||
// It expects package to be already downloaded. | ||
func (i *Installer) Install(programName, version, installDir string) error { | ||
if err := i.installer.Install(programName, version, installDir); err != nil { | ||
return err | ||
} | ||
|
||
// post install hooks | ||
nameLower := strings.ToLower(programName) | ||
_, isSupported := program.SupportedMap[nameLower] | ||
if !isSupported { | ||
return nil | ||
} | ||
|
||
for _, spec := range program.Supported { | ||
if strings.ToLower(spec.Name) != nameLower { | ||
continue | ||
} | ||
|
||
if spec.PostInstallSteps != nil { | ||
return spec.PostInstallSteps.Execute(installDir) | ||
} | ||
|
||
// only one spec for type | ||
break | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I this is what I hate the most about the yaml package, there is a way to work around that go-yaml/yaml#13 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove my name next to NOTE :)