Capistrano multistage variables - capistrano

When using ext/multistage, why aren't the variables that are set in a stage (production.rb) available in deploy.rb?
In production.rb: set :domain, "domain.com"
In deploy.rb: set :vhost, "/var/www/#{domain}"
But when I try to run, it complains
undefined local variable or method `domain' for #<Capistrano::Configuration:0x00000100d07248> (NameError)

This seems very silly, and I'm probably not doing this right, but it does work if I defer the variable setting in deploy.rb like this:
set(:stage_domain) { "#{domain}" }
set(:vhost) { "/var/www/#{stage_domain}" }
set(:repo_dir) { "#{vhost}/repository" }
set(:deploy_to) { "#{repo_dir}" }
set(:httpdocs_link) { "#{deploy_to}" }
role(:web) { stage_domain }

Related

How to determine if Terminal supports ANSI with SBT?

With an old version of SBT I use to do this:
initialize := {
if (sbt.internal.util.ConsoleAppender.formatEnabledInEnv) {
println("My fancy ANSI long string with color")
}
}
But this is now deprecated at the new SBT requesting the usage of Terminal.isAnsiSupported but I don't know how to access that value.
Maybe with something like this but it triggers an error:
initialize := {
if (terminal.isAnsiSupported) { // <- ERROR
println("My fancy ANSI long string with color")
}
}
Thanks for any tips and help about this.
OK,found it !! ... after couple of tries is like this:
initialize := {
if (sbt.internal.util.Terminal.console.isAnsiSupported && sbt.internal.util.Terminal.console.isColorEnabled) {
println("My fancy ANSI long string with color")
}
}
The trick is to use .console inside the internal util Terminal of SBT.

How to Check if a SYSTEM environment variable exists?

There is USER Environment Variables, and SYSTEM environment variables.
How can i make this condition check ONLY for SYSTEM env variable if it exists?
if (-not Test-Path 'env:VariableX ') {
"doesnt exist!"
}
this would not return anything if VariableX is defined as a USER variable. i want to ignore that and only focus on if its defined in SYSTEM env variables
This would work:
if (-not [Environment]::GetEnvironmentVariable('VariableX', 'Machine'))
{
"doesnt exist!"
}

how do i get a variable out of powershell in jenkins declarative pipeline?

steps {
script{
env.StorysTested = ''
try{
powershell('''
//some code here
foreach ( $item in $Comments )
{
//some code here
//assigning a new value to StoryTested env variable
$env:StorysTested = "some value"
}
//below line works fine and displays the value
Write-Output "Stories tested : $env:StorysTested"
''')
//below null value is displayed for StorysTested``
echo " From Grrovy : ${env.StorysTested}"
}
catch(err)
{
throw err
}
}
I am using a jenkins declarative pipeline.
In the above code i m trying to use the value of $env:StorysTested in groovy which was assigned in powershell. Is there any way i can retain a variable value that was assigned in powershell, after the powershell execution is over. storing it in env variable was one way i thought of but clearly that didnt work.
If you set an environment variable using $env:StorysTested = "some value", this variable is stored for the powershell process and is not permanent or visible outside this process.
To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method:
[Environment]::SetEnvironmentVariable("StorysTested", "some value", "User")
or
[Environment]::SetEnvironmentVariable("StorysTested", "some value", "Machine")
To delete from within PowerShell, you use the same .NET method and assign a $null value to the variable like this:
[Environment]::SetEnvironmentVariable("StorysTested",$null,"User") # or "Machine" of course
Hope that helps

Run powershell commands atomically

I have 3 powershell commands that need to run synchronously, as I need to pass the output from the 1st to the 2nd, and so on. However if any fail I need to back out of all 3. Is there a cmdlet or technique, even, that will enable this behavior in powershell?
You could wrap each of your commands in an object or a hashtable with a corresponding "rollback" action and a test:
$Commands = #(
#{
Action = { Set-Value $PathOne "newValue" }
RollBack = { Set-Value $PathOne "oldValue" }
Test = { Test-Something }
}
#{
Action = { Set-Value $PathTwo "newValue" }
RollBack = { Set-Value $PathTwo "oldValue" }
Test = { Test-Something }
}
)
Then use a Stack<Scriptblock> to keep track of actions that need to be executed in the event of a "roll-back"
$RollbackStack = [System.Collections.Generic.Stack[Scriptblock]]::new()
foreach($Command in $Commands){
# Execute command
& $Command.Action
# Add rollback action to stack
$RollbackStack.Push($Command.Rollback)
# Test result, roll back if failed
if(-not(& $Command.Test)){
foreach($rollbackAction in $RollbackStack){
& $rollbackAction
}
}
}
In general, PowerShell commands do not support "backing out" or "rollback". The Registry provider does support transactions (which would allow this kind of behavior), but only with operations involving the registry.
It might be possible to capture the state of things that you're updating in the commands and re-applying those states, but it would be a very manual process.

more than one defaction in the body?

All, can I run more than one defaction in the body of the rule? or can I only run one?
You can define as many actions in the pre block of a rule as you want. You can have as many actions in the action block of a rule as you want (just enclose the action block in curly braces). For example,
rule first_rule {
select when pageview ".*" setting ()
pre {
notify_one = defaction() { notify("notify_one", "First defaction"); };
notify_two = defaction() { notify("notify_two", "Second defaction"); };
}
{
notify_one();
notify_two();
}
}
So I think the answer to your question is yes.
Your question is a little confusing, but I'll give it a run.
Running actions defined with defaction is just like running system defined actions.
If you want to run more then one action in a rule, you need to wrap them in {} like so:
rule foo {
select when pageview ".*"
{
notify("cheese", "brie");
notify("apple", "golden delicious");
}
}
I seem to recall that a defaction has an implicit, optional 'pre' section, followed by the action(s). To include multiple actions you do need {} as Sam says.
act1 = defaction() {
{
notify("Defaction Demo", "<ul id='demo_id'></ul>");
append("#demo-id", "<li>cheese: brie</li>");
append("#demo-id", "<li>apple: golden delicious</li>");
}
};
That works out to defaction() { { ... } }; but the extra curly braces are required if you want more than one action in a defaction.
See also http://docs.kynetx.com/docs/Defaction