Need to pass connection string for update in database for IIS deployment

Hello,

I need to pass some connection string to update my web.config file for the IIS deployment.

I tried various method using xml parameters but no luck.

Need a help that how can i pass the connection string using the recipe.

Thanks in advance

We wrote a custom resource that wraps around the iis_config resource. The iis_config basically wraps appcmd.exe

You could use the iis_config resource directly in your recipes, but appcmd.exe is hard to make idempotent. By wrapping iis_config in a custom resource, it ensures appcmd.exe is executed only when needed.

Using IIS cookbook 4.1.9 (some things may have changed in the IIS 5.0.0 cookbook)

In the recipe

foo_session 'bar' do
  site 'foobar'
  action :nothing
end

resources/foo_session.rb

resource_name :foo_session
property :site, String, required: true

default_action :sql


action :redis do
  # appcmd.exe clear config 'MySite/foobar' '-section:system.web/sessionState'
  iis_config 'foobar session clear' do
    cfg_cmd "MySite/#{node['mycookbook']['site']} -section:system.web/sessionState"
    action :clear
  end

  # appcmd.exe set config 'MySite/foobar' '-section:system.web/sessionState' '-mode:Custom' '-timeout:90' '-customProvider:RedisSessionStateStoreProvider'
  iis_config 'session redis' do
    cfg_cmd "MySite/#{site} -section:system.web/sessionState \"-mode:#{node['mycookbook']['session_mode']}\" \"-customProvider:#{node['mycookbook']['session_custom_provider']}\" \"-timeout:#{node['mycookbook']['session_timeout']}\""
    action :set
  end
end

action :sql do
  # appcmd clear config 'MySite/foobar' '-section:system.web/sessionState'
  iis_config 'session clear' do
    cfg_cmd "MySite/#{node['mycookbook']['site']} -section:system.web/sessionState"
    action :clear
  end

  # appcmd.exe set config 'MySite/foobar' '-section:system.web/sessionState' '-mode:SQLServer' '-allowCustomSqlDatabase:true' '-sqlConnectionString:network=xxxxx; initial catalog=xxxxxx; user id=xxxxx; connection lifetime=xxxx;max pool size=xxxx;data source=xxxxx; failover partner=xxxxxx; password=xxxxxx' '-cookieless:xxxx' '-timeout:90'
  iis_config 'session sql' do
    cfg_cmd "MySite/#{node['mycookbook']['site']} -section:system.web/sessionState \"-mode:#{node['mycookbook']['session_mode']}\" \"-allowCustomSqlDatabase:#{node['mycookbook']['session_allowcustomdatabase']}\" \"-sqlConnectionString:#{node['mycookbook']['session_sqlconnectionstring']}\" \"-cookieless:#{node['mycookbook']['session_cookieless']}\" \"-timeout:#{node['mycookbook']['session_timeout']}\""
    action :set
  end
end

Then when doing a deploy, the foo_session recipe is ‘notified’ with either a redis or sql action.

windows_zipfile 'unpack_foo' do
  source c:\somezip.zip
  path c:\somelocation
  notifies 'redis', 'foo_session[bar]', :delayed
end

@spuder …Thanks a lot for your reply and help…

I have already written a cookbook for the successful deployment wherein site ,application, pool and the folder structure under the site is successfully done.

Now while passing the connection string parameters using the recipe I could not update my SQL server.

So according to you do I need to define the connection string parameter in resources of my cookbook.

I did not get the clear picture can you please little more.I will be thankful for your act of kindness.

Thanks in advance

I’ve updated my previous message to make this a little more clear.

The attributes are variables that are set at the role/environment level

"my-cookbook": {
  "session": "redis",
  "site": "foobar",
  "session_mode": "Custom",
  "timeout": 90,
  "session_custom_provider": "RedisStateStoreProvider"
}

The cookbook takes those attributes, and generates the correct appcmd.exe command

before

appcmd.exe set config 'MySite/foobar' '-section:system.web/sessionState' '-mode:#{node['my-cookbook']['session_mode']}' '-timeout:#{node['my-cookbook']['timeout']}' '-customProvider:#{node['my-cookbook']['session_custom_provider']}'

after

appcmd.exe set config 'MySite/foobar' '-section:system.web/sessionState' '-mode:Custom' '-timeout:90' '-customProvider:RedisSessionStateStoreProvider'

So according to you do I need to define the connection string parameter in resources of my cookbook.

Yes, the connection string needs to either be hard coded in the cookbook, or generated using variables.

@spuder

Thanks a lot…

This makes a lot of sense to me.

Let me try out this in my environment.If any issue I will get back to you.

Thanks a lot once again