Skip to content

Commit

Permalink
Add optional init.sh reload (#52)
Browse files Browse the repository at this point in the history
* Add optional init.sh reload

* Update Example_sls for Reloadable
  • Loading branch information
bmoylan authored and nmiyake committed Feb 5, 2017
1 parent 688c01e commit adfe8e2
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
68 changes: 68 additions & 0 deletions apps/distgo/cmd/dist/dist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,74 @@ daemon: true
assert.Regexp(t, `SERVICE_CMD="\$SERVICE_HOME/service/bin/\$OS_ARCH/\$SERVICE providedArgs arg2"\n`, string(bytes), "Case %d: %s", caseNum, name)
},
},
{
name: "properly templatizes init.sh with Reloadable: false",
spec: func(projectDir string) params.ProductBuildSpecWithDeps {
specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(
projectDir,
"foo",
git.ProjectInfo{
Version: "0.1.0",
},
params.Product{
Build: params.Build{
MainPkg: "./.",
},
Dist: []params.Dist{{
Info: &params.SLSDistInfo{},
}},
},
params.Project{
GroupID: "com.test.group",
},
), nil)
require.NoError(t, err)
return specWithDeps
},
preDistAction: func(projectDir string, buildSpec params.ProductBuildSpec) {
gittest.CreateGitTag(t, projectDir, "0.1.0")
},
validate: func(caseNum int, name string, projectDir string) {
bytes, err := ioutil.ReadFile(path.Join(projectDir, "dist", "foo-0.1.0", "service", "bin", "init.sh"))
require.NoError(t, err)
assert.Regexp(t, `does not support reload`, string(bytes), "Case %d: %s", caseNum, name)
},
},
{
name: "properly templatizes init.sh with Reloadable: true",
spec: func(projectDir string) params.ProductBuildSpecWithDeps {
specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(
projectDir,
"foo",
git.ProjectInfo{
Version: "0.1.0",
},
params.Product{
Build: params.Build{
MainPkg: "./.",
},
Dist: []params.Dist{{
Info: &params.SLSDistInfo{
Reloadable: true,
},
}},
},
params.Project{
GroupID: "com.test.group",
},
), nil)
require.NoError(t, err)
return specWithDeps
},
preDistAction: func(projectDir string, buildSpec params.ProductBuildSpec) {
gittest.CreateGitTag(t, projectDir, "0.1.0")
},
validate: func(caseNum int, name string, projectDir string) {
bytes, err := ioutil.ReadFile(path.Join(projectDir, "dist", "foo-0.1.0", "service", "bin", "init.sh"))
require.NoError(t, err)
assert.Regexp(t, `Reloading`, string(bytes), "Case %d: %s", caseNum, name)
},
},
{
name: "creates outputs using bin mode",
spec: func(projectDir string) params.ProductBuildSpecWithDeps {
Expand Down
19 changes: 18 additions & 1 deletion apps/distgo/cmd/dist/slsdist.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,25 @@ restart)
service/bin/init.sh stop
service/bin/init.sh start
;;
reload){{if .Dist.Reloadable}}
printf "%-50s" "Reloading '$SERVICE'..."
if service/bin/init.sh status > /dev/null 2>&1; then
PID=$(cat $PIDFILE)
if ! kill -s HUP $PID; then
printf "%s\n" "Failed to send HUP ($PID)"
exit 1
fi
printf "%s\n" "Reloaded ($PID)"
else
printf "%s\n" "Service not running"
exit 7
fi
{{else}}
printf "%s\n" "'$SERVICE' does not support reload"
exit 3
{{end}};;
*)
echo "Usage: $0 {status|start|stop|console|restart}"
echo "Usage: $0 status|start|stop|console|restart{{if .Dist.Reloadable}}|reload{{end}}"
exit 1
esac
`
Expand Down
5 changes: 5 additions & 0 deletions apps/distgo/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ type SLSDist struct {
// ManifestExtensions contain the SLS manifest extensions for the distribution.
ManifestExtensions map[string]interface{} `yaml:"manifest-extensions" json:"manifest-extensions"`

// Reloadable will enable the `init.sh reload` command which sends SIGHUP to the process.
Reloadable bool `yaml:"reloadable" json:"reloadable"`

// YMLValidationExclude specifies a matcher used to specify YML files or paths that should not be validated as
// part of creating the distribution. By default, the SLS distribution task verifies that all "*.yml" and
// "*.yaml" files in the distribution are syntactically valid. If a distribution is known to ship with YML files
Expand Down Expand Up @@ -393,6 +396,7 @@ func (cfg *DistInfo) ToParam() (params.DistInfo, error) {
ServiceArgs: val.ServiceArgs,
ProductType: val.ProductType,
ManifestExtensions: val.ManifestExtensions,
Reloadable: val.Reloadable,
YMLValidationExclude: val.YMLValidationExclude.Matcher(),
}
case params.BinDistType:
Expand Down Expand Up @@ -451,6 +455,7 @@ func (cfg *SLSDist) ToParams() params.SLSDistInfo {
ServiceArgs: cfg.ServiceArgs,
ProductType: cfg.ProductType,
ManifestExtensions: cfg.ManifestExtensions,
Reloadable: cfg.Reloadable,
YMLValidationExclude: cfg.YMLValidationExclude.Matcher(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions apps/distgo/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestReadConfig(t *testing.T) {
info:
manifest-template-file: resources/input/manifest.yml
product-type: service.v1
reloadable: true
yml-validation-exclude:
names:
- foo
Expand Down Expand Up @@ -112,6 +113,7 @@ echo "main.year=$YEAR"
Info: config.SLSDist{
ManifestTemplateFile: "resources/input/manifest.yml",
ProductType: "service.v1",
Reloadable: true,
YMLValidationExclude: matcher.NamesPathsCfg{
Names: []string{"foo"},
Paths: []string{"bar"},
Expand Down
3 changes: 2 additions & 1 deletion apps/distgo/config/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ products:
service-args: "--config var/conf/cache.yml server"
manifest-extensions:
cache: true
reloadable: true
group-id: com.palantir.cache`

cfg := configFromYML(yml)
fmt.Printf("%q", fmt.Sprintf("%+v", cfg))
// Output: "{Products:map[cache-service:{Build:{Script: MainPkg:./main/cache OutputDir: BuildArgsScript: VersionVar:main.Version Environment:map[] OSArchs:[linux-amd64]} Run:{Args:[]} Dist:[{OutputDir:cache/build/distributions InputDir:cache/dist/sls InputProducts:[] Script: DistType:{Type:sls Info:{InitShTemplateFile: ManifestTemplateFile: ServiceArgs:--config var/conf/cache.yml server ProductType: ManifestExtensions:map[cache:true] YMLValidationExclude:{Names:[] Paths:[]}}} Publish:{GroupID: Almanac:{Metadata:map[] Tags:[]}}}] DefaultPublish:{GroupID: Almanac:{Metadata:map[] Tags:[]}}}] BuildOutputDir: DistOutputDir: DistScriptInclude: GroupID:com.palantir.cache Exclude:{Names:[] Paths:[]}}"
// Output: "{Products:map[cache-service:{Build:{Script: MainPkg:./main/cache OutputDir: BuildArgsScript: VersionVar:main.Version Environment:map[] OSArchs:[linux-amd64]} Run:{Args:[]} Dist:[{OutputDir:cache/build/distributions InputDir:cache/dist/sls InputProducts:[] Script: DistType:{Type:sls Info:{InitShTemplateFile: ManifestTemplateFile: ServiceArgs:--config var/conf/cache.yml server ProductType: ManifestExtensions:map[cache:true] Reloadable:true YMLValidationExclude:{Names:[] Paths:[]}}} Publish:{GroupID: Almanac:{Metadata:map[] Tags:[]}}}] DefaultPublish:{GroupID: Almanac:{Metadata:map[] Tags:[]}}}] BuildOutputDir: DistOutputDir: DistScriptInclude: GroupID:com.palantir.cache Exclude:{Names:[] Paths:[]}}"
}

func Example_bin() {
Expand Down
3 changes: 3 additions & 0 deletions apps/distgo/params/dist.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type SLSDistInfo struct {
// ManifestExtensions contain the SLS manifest extensions for the distribution.
ManifestExtensions map[string]interface{}

// Reloadable will enable the `init.sh reload` command which sends SIGHUP to the process.
Reloadable bool

// YMLValidationExclude specifies a matcher used to specify YML files or paths that should not be validated as
// part of creating the distribution. By default, the SLS distribution task verifies that all "*.yml" and
// "*.yaml" files in the distribution are syntactically valid. If a distribution is known to ship with YML files
Expand Down

0 comments on commit adfe8e2

Please sign in to comment.