I have python script generating AWS Signature key for S3. It generates two values:
GZkXNl6Leat71ckcwfxGuiHxt9fnkj47F1SbVjRu/t0=
20190129/eu-west-2/s3/aws4_request
Both are valid for 7 days. What I want is to run that script for every five days using cron inside the Docker container, grab the output and place/replace values in the Nginx config
config:
server {
listen 80;
aws_access_key 'AKIDEXAMPLE';
aws_signing_key FIRST_VALUE;
aws_key_scope SECOND_VALUE;
aws_s3_bucket s3_bucket_name;
location / {
aws_sign;
proxy_pass http://s3_bucket_name.s3.amazonaws.com;
}
Then restart nginx in the container
Assuming the values are stored in val_file, slotting them into nginx.conf, this simplistic solution ought to do -
$: cat script
#! /bin/env bash
{ read -r val1 # read the first line
read -r val2 # read the second line
sed -i "s!FIRST_VALUE!$val1!;
s!SECOND_VALUE!$val2!;
" nginx.conf
} < val_file
$: script
$: cat nginx.conf
server {
listen 80;
aws_access_key 'AKIDEXAMPLE';
aws_signing_key GZkXNl6Leat71ckcwfxGuiHxt9fnkj47F1SbVjRu/t0=;
aws_key_scope 20190129/eu-west-2/s3/aws4_request;
aws_s3_bucket s3_bucket_name;
location / {
aws_sign;
proxy_pass http://s3_bucket_name.s3.amazonaws.com;
}
The curlies make the single input supply both reads. Then it's just a sed, using !'s because you have standard forward slashes in your data. Double quotes on the sed to allow embedded vars - not ideal, but seems ok here.
I have a script where i declare variables then create a file and then replace a variable within that file, this is my example script
#!/bin/bash
DMNAME = mydomain.com
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DMNAME/privkey.pem;
EOF
sed -i 's/DMNAME/mydomain.com/g' /etc/nginx/conf.d/default.conf
#
Would this be the correct way of replacing DMNAME with mydomain.com ?
#!/bin/bash
DMNAME="mydomain.com"
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name $DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/$DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DMNAME/privkey.pem;
EOF
#Bor is right that he fills /etc/nginx/conf.d/default.conf with the correct values when creating it.
When you want to use default.conf more than once, you shouldn't let sed change the file with the -i option, but redirect the results to the file you want.
# Answer: do not use this here: DMNAME = mydomain.com
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DMNAME/privkey.pem;
EOF
# And now DMNAME="mydomain.com" (without spaces, quotes are optional here).
# or for different domains
confpath="/etc/nginx/conf.d"
for domain in mydomain.com yourdomain.com hisdomain.com; do
sed "s/DMNAME/${domain}/g" "${confpath}"/default.conf > "${confpath}"/${domain%.*}.conf
done
I wrote a setup script for something and now I need to create a virtualhost with it. To do so I used this code:
echo -e \
"WSGISocketPrefix $DIRECTORY/socks/\n"\
"WSGIPythonHome $DIRECTORY/env/local\n"\
"WSGIRestrictStdout On\n"\
"WSGIRestrictSignal Off\n"\
"WSGIPythonOptimize 1\n"\
"<VirtualHost *:80>\n"\
" ServerAdmin root#localhost.com\n"\
" ServerName app.localhost\n"\
" DocumentRoot \"$DIRECTORY\"\n"\
" Alias /m/ $DIRECTORY/static/\n"\
" Alias /upfiles/ $DIRECTORY/askbot/upfiles/\n"\
" <DirectoryMatch \"$DIRECTORY/askbot/skins/([^/]+)/media\">\n"\
" Order deny,allow\n"\
" Allow from all\n"\
" </DirectoryMatch>\n"\
" <Directory \"$DIRECTORY/askbot/upfiles\">\n"\
" Order deny,allow\n"\
" Allow from all\n"\
" </Directory>\n"\
"\n"\
" WSGIDaemonProcess askbot_"$NUMBER"_\n"\
" WSGIProcessGroup askbot_"$NUMBER"_\n"\
" WSGIScriptAlias / $DIRECTORY/django.wsgi\n"\
"\n"\
' ErrorLog ${APACHE_LOG_DIR}/askbot_error.log'"\n"\
' CustomLog ${APACHE_LOG_DIR}/askbot_access.log combined'"\n"\
"</VirtualHost>\n" > /etc/apache2/sites-available/app.conf
$DIRECTORY is a variable containing the path and therefore its content should be printed. ${APACHE_LOG_DIR} however is no variable here and should be printed as is. Unfortunately instead of writing the content to the file it will echo it to the terminal with some errors (File not found etc). When I remove the last two lines it does work but of course thats not a solution but I cant seem to get it to work.
Any ideas?
Use a here-document. Use \ to escape the dollar signs where needed.
cat <<EOF > /etc/apache2/sites-available/app.conf
WSGISocketPrefix $DIRECTORY/socks/
WSGIPythonHome $DIRECTORY/env/local
WSGIRestrictStdout On
WSGIRestrictSignal Off
WSGIPythonOptimize 1
<VirtualHost *:80>
ServerAdmin root#localhost.com
ServerName app.localhost
DocumentRoot "$DIRECTORY"
Alias /m/ $DIRECTORY/static/
Alias /upfiles/ $DIRECTORY/askbot/upfiles/
<DirectoryMatch "$DIRECTORY/askbot/skins/([^/]+)/media">
Order deny,allow
Allow from all
</DirectoryMatch>
<Directory "$DIRECTORY/askbot/upfiles">
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess askbot_$NUMBER_
WSGIProcessGroup askbot_$NUMBER_
WSGIScriptAlias / $DIRECTORY/django.wsgi
ErrorLog \${APACHE_LOG_DIR}/askbot_error.log
CustomLog \${APACHE_LOG_DIR}/askbot_access.log combined
</VirtualHost>
EOF
echo understands multiline strings:
echo "
fist section here with ${substitutions}
" > /etc/apache2/sites-available/app.conf
then append the last section:
echo '
second section here without substitutions
' >> /etc/apache2/sites-available/app.conf
but in this case it might be easier to create a template file and then use e.g. sed to do string substitutions? I use the commandline interface to Jinja2 to do the same task (https://github.com/kolypto/j2cli), but that is almost certainly overkill for such a simple template ;-)
On my codeigniter 3 project on wamp, I have a sub domain folder called cms-2 which has it's own index.php file cms-1 > cms-2 > index.php.
cms-1
cms-1 > application
cms-1 > cms-2
cms-1 > cms-2 > index.php
cms-1 > image
cms-1 > system
cms-1 > index.php
If I echo the FCPATH when I am on the sub domain it shows.
C:\wamp\www\codeigniter\cms-1\cms-2/
But I would like to know on that index.php file belonging to that sub domain instead of FCPATH showing
C:\wamp\www\codeigniter\cms-1\cms-2/
Would like it to show
C:\wamp\www\codeigniter\cms-1/
Question How can I make the define('FCPATH', dirname(__FILE__) .'/'); for the subdomain index.php only show C:\wamp\www\codeigniter\cms-1/ when echo FCPATH
Have got it working now here how I solved it. On the sub domain / sub directory index.php
I have had to make a couple of variables to make FCPATH produce what I wanted
$get_current_path_to_front = str_replace('\\', '/', realpath(dirname(__FILE__))) . '/';
$set_new_path_to_front = str_replace('\\', '/', realpath($get_current_path_to_front . '../')) . '/';
//echo $set_new_path_to_front;
// Path to the front controller (this file)
define('FCPATH', $set_new_path_to_front);
echo FCPATH;
So now when I echo FCPATH on sub domain produces
C:\wamp\www\codeigniter\cms-1/
I'm encountering an issue where a command (a drush profile, to be exact) needs to be run within a created directory automatically. I can't find much advisement within the script itself running it as an argument, and the logic i've been instructed by my superiors seems to suggest this as the best approach.
Here is what the bash script does:
It creates the directory structure and some Apache2 files for a Drupal install
It creates a rudimentary Drush profile that is then used by the utility to
Autoinstall Drupal and modules by running the alias "standardprofile" within a directory containing this profile file
The actual commands I use to run the script from Bash (full scripts below) are
cd /var/www/$DOMAINNAME-dev.$URL
eval "standardprofile"
The above lines don't run the script (or anything past the first starting line, and then then bash script resumes) Is there a better way to instruct a bash script to run a command from the specified directory and then resume once any other utilities being run in the foreground finish?
#!/bin/bash
# Creates the proper staging and development environment for a site
# Init
URL=example.com #URL used in creating directories
OMIT_STAGING="n" #Set to true if omitting staging
DOMAINNAME=""
###
# FUNCTIONS
###
function generate_empty_dirs {
mkdir /var/www/$1-staging.$URL
mkdir /var/www/$1-dev.$URL
mkdir /var/www/$1-staging.$URL/logs
mkdir /var/www/$1-staging.$URL/public_html
mkdir /var/www/$1-dev.$URL/logs
mkdir /var/www/$1-dev.$URL/public_html
}
function generate_drupal_staging_apacheconf {
echo "<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName $1-staging.$URL
DocumentRoot /var/www/$1-staging.$URL/public_html
<Directory /var/www/$1-staging.$URL/public_html>
Options -MultiViews +ExecCGI
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=\$1 [L,QSA]
</Directory>
LogLevel warn
ErrorLog /var/www/$1-staging.$URL/logs/error.log
CustomLog /var/www/$1-staging.$URL/logs/access.log combined
# enable PHP error logging
php_flag log_errors on
php_value error_log /var/www/$1-staging.$URL/logs/php_errors.log
# Possible LogLevel values include: debug, info, notice, warn, error, crit,
# alert, emerg.
</VirtualHost>" > /etc/apache2/sites-available/$1-staging.$URL
}
function generate_drupal_dev_apacheconf {
echo "<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName $1-dev.$URL
DocumentRoot /var/www/$1-dev.$URL/public_html
<Directory /var/www/$1-dev.$URL/public_html>
Options -MultiViews +ExecCGI
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=\$1 [L,QSA]
</Directory>
LogLevel warn
ErrorLog /var/www/$1-dev.$URL/logs/error.log
CustomLog /var/www/$1-dev.$URL/logs/access.log combined
# enable PHP error logging
php_flag log_errors on
php_value error_log /var/www/$1-dev.$URL/logs/php_errors.log
# Possible LogLevel values include: debug, info, notice, warn, error, crit,
# alert, emerg.
</VirtualHost>" > /etc/apache2/sites-available/$1-dev.$URL
}
function generate_wordpress_staging_apacheconf {
echo "<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName $1-staging.$URL
DocumentRoot /var/www/$1-staging.$URL/public_html
<Directory /var/www/$1-staging.$URL/public_html>
Options -MultiViews +ExecCGI
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
LogLevel warn
ErrorLog /var/www/$1-staging.$URL/logs/error.log
CustomLog /var/www/$1-staging.$URL/logs/access.log combined
# enable PHP error logging
php_flag log_errors on
php_value error_log /var/www/$1-staging.$URL/logs/php_errors.log
# Possible LogLevel values include: debug, info, notice, warn, error, crit,
# alert, emerg.
</VirtualHost>" > /etc/apache2/sites-available/$1-staging.$URL
}
function generate_wordpress_dev_apacheconf {
echo "<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName $1-dev.$URL
DocumentRoot /var/www/$1-dev.$URL/public_html
<Directory /var/www/$1-dev.$URL/public_html>
Options -MultiViews +ExecCGI
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
LogLevel warn
ErrorLog /var/www/$1-dev.$URL/logs/error.log
CustomLog /var/www/$1-dev.$URL/logs/access.log combined
# enable PHP error logging
php_flag log_errors on
php_value error_log /var/www/$1-dev.$URL/logs/php_errors.log
# Possible LogLevel values include: debug, info, notice, warn, error, crit,
# alert, emerg.
</VirtualHost>" > /etc/apache2/sites-available/$1-dev.$URL
}
function generate_drupal_drush_dev_profile {
# Dev
echo " <?php
\$db_engine = 'mysql';
\$db_name = '$1_db';
\$db_user = '$1_user';
\$db_pw = '1password';
\$db_su = 'root';
\$db_su_pw = '';
\$site_name = '$1';
\$account_name = 'admin';
\$account_pw = 'example';
\$account_mail = 'example#example.com';
\$site_mail = \$account_mail;" > /var/www/$1-dev.$URL/installsettings.php
}
function generate_drupal_drush_staging_profile {
#Staging
echo " <?php
\$db_engine = 'mysql';
\$db_name = '$1_db';
\$db_user = '$1_user';
\$db_pw = '1password';
\$db_su = 'root';
\$db_su_pw = '';
\$site_name = '$1';
\$account_name = 'admin';
\$account_pw = 'example';
\$account_mail = 'example#example.com';
\$site_mail = \$account_mail;" > /var/www/$1-staging.$URL/installsettings.php
}
function alter_group_owner {
chown -R www-data /var/www/$1-staging.$URL
chgrp -R www-data /var/www/$1-staging.$URL
chown -R www-data /var/www/$1-dev.$URL
chgrp -R www-data /var/www/$1-dev.$URL
chown -R www-data /etc/apache2/sites-available/$1-staging.$URL
chgrp -R www-data /etc/apache2/sites-available/$1-staging.$URL
chown -R www-data /etc/apache2/sites-available/$1-dev.$URL
chgrp -R www-data /etc/apache2/sites-available/$1-dev.$URL
}
###
# BEGIN MAIN LOGIC
###
# Checks to see if user is Root or is using Sudo, otherwise exit
if [ $(id -u) != 0 ]
then
echo "You must have elevation to run this script."
exit 1
fi
# Prompts for name of site
if [ -z "$1" ]
then
read -p "Domain for the new site: " DOMAINNAME
if [ ! -z $DOMAINNAME ]
then
echo "Generating the empty directories now..."
generate_empty_dirs $DOMAINNAME
else
echo "You must provide a valid domain name"
exit 1
fi
else
generate_empty_dirs $1
DOMAINNAME=$1
fi
# Determine the kind of site being generated
read -p "Is this a (D)rupal or a (W)ordPress site? " SITETYPE
read -p "Do you need both the dev and staging sites? [Y/N] (case sensitive): " NEEDDIRS
if [ -n "$SITETYPE" ]
then
if [ "$SITETYPE" == "D" ]
then
generate_drupal_dev_apacheconf $DOMAINNAME
generate_drupal_drush_dev_profile $DOMAINNAME
rm /var/www/$DOMAINNAME-dev.$URL/public_html
rm /var/www/$DOMAINNAME-dev.$URL/logs
cd /var/www/$DOMAINNAME-dev.$URL
eval "standardprofile"
if [ "$NEEDDIRS" == "Y" ]
then
generate_drupal_staging_apacheconf $DOMAINNAME
generate_drupal_drush_staging_profile $DOMAINNAME
cd /var/www/$DOMAINNAME-staging.$URL
eval "standardprofile"
OMIT_STAGING="n"
else
OMIT_STAGING="y"
fi
elif [ "$SITETYPE" == "W" ]
then
generate_wordpress_dev_apacheconf $DOMAINNAME
if [ "$NEEDDIRS" == "Y" ]
then
generate_wordpress_staging_apacheconf $DOMAINNAME
OMIT_STAGING="n"
else
OMIT_STAGING="y"
fi
else
echo "Invalid option provided"
exit 1
fi
else
echo "No option provided"
exit 1
fi
alter_group_owner $DOMAINNAME
# Load the new confs into Apache
if [ "$OMIT_STAGING" == "y" ]
then
a2ensite $DOMAINNAME-dev.$URL
rm -rf /var/www/$DOMAINNAME-staging.$URL
else
a2ensite $DOMAINNAME-dev.$URL
a2ensite $DOMAINNAME-staging.$URL
fi
# Reload apache
service apache2 reload
# All done
echo "Sites created at domains:"
if [ "$OMIT_STAGING" == "y" ]
then
echo $DOMAINNAME-dev.$URL
else
echo $DOMAINNAME-dev.$URL
echo $DOMAINNAME-staging.$URL
fi
exit
#End of script
And the drush script
#!/usr/bin/env drush
$a = drush_get_arguments();
$current_directory = getcwd();
$profiles = substr($a[1], 0, strlen($a[1])-15) . 'standard.make';
if(file_exists($current_directory . '/installsettings.php')) {
require_once($current_directory . '/installsettings.php');
}
drush_print("Time to prepare our site install...");
if(!file_exists('logs')) {
drush_op_system('mkdir logs');
}
if(!file_exists('public_html')) {
$prev = drush_get_context('DRUSH_AFFIRMATIVE');
drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
drush_invoke('make', array($profiles, 'public_html'));
drush_op_system('cp public_html/sites/all/modules/services/servers/rest_server/lib/spyc/spyc.php public_html/sites/all/modules/services/servers/rest_server/lib/spyc.php');
#drush_shell_exec('cd public_html');
#shell_exec('cd public_html');
chdir('public_html');
drush_invoke_process("#self","site-install",null,array(
'db-url' => $db_engine . "://" . $db_user . ":" . $db_pw . "#localhost/" . $db_name,
'account-name' => $account_name,
'account-pass' => $account_pw,
'account-email' => $account_mail,
'db-su' => $db_su,
'db-su-pw' => $db_su_pw,
'site-mail' => $site_mail,
'site-name' => $site_name,
'clean-url' => FALSE,
));
drush_invoke_process("#self","pm-enable",
array('ctools', 'views', 'views_ui', 'features', 'strongarm', 'fe_block',
'entity', 'token', 'module_filter', 'pathauto', 'devel', 'simplehtmldom',
'services', 'rest_server', 'entity_token', 'date', 'date_api', 'date_tools',
'date_views', 'date_popup','rules', 'rules_admin', 'views_slideshow',
'views_slideshow_cycle', 'strongarm', 'diff', 'auto_nodetitle', 'libraries',
'realname', 'views_php'),
array('root' => $current_directory . '/public_html'));
drush_set_context('DRUSH_AFFIRMATIVE', $prev);
}
Is there a better way to instruct a bash script to run a command from the specified directory and then resume once any other utilities being run in the foreground finish?
Sure:
( cd $dir && cmd )
will run cmd from the specified directory. Since the two commands are run in a subshell, the script will resume in its present directory when the subshell completes.
This should work:
cd /var/www/$DOMAINNAME-dev.$URL
/usr/bin/env drush "standardprofile"