• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

Archives for May 2017

SSH To A Docker Container

May 24, 2017

Docker containers are awesome. Docker allows you to quickly create development environments in a matter of minutes. Docker gives you the ability to package, ship and share your docker image to anyone. Once your docker image is on the Docker repository, anyone can pull it down and run it on their own operating system.

To keep track of running containers on your system, you can type ‘docker ps -a’ on your terminal.

$ docker ps -a
# gives a result similar to this ...
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f322d2370415        moul/icecast        "/start.sh"              2 hours ago         Up 2 hours          0.0.0.0:8000->8000/tcp   icecast_icecast_1

$ docker ps -a # gives a result similar to this ... CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f322d2370415 moul/icecast "/start.sh" 2 hours ago Up 2 hours 0.0.0.0:8000->8000/tcp icecast_icecast_1

If you would like to SSH to a running Docker container, just run the following from the terminal.

docker exec -t -i f322d2370415 /bin/bash
# it will take you to this ... 
root@f322d2370415:/#

docker exec -t -i f322d2370415 /bin/bash # it will take you to this ... root@f322d2370415:/#

f322d2370415 is the Container ID, while /bin/bash is the shell that you would like to use.

Filed Under: Cloud Tagged With: docker, ssh

Start EC2 AMI from AWS CLI

May 24, 2017

Amazon Web Services has an API via a CLI (command line interface) which give users the ability to manage servers from a remote host. The AWS CLI must be installed and authenticated to AWS on the host computer. Once a user is logged in to AWS, they can perform certain management tasks such as starting and stopping EC2 instances.

How to start EC2 instance from AWS CLI

Requires an image id, instance count, instance type, key and security group.

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type c4.2xlarge --key-name your-key  —security-group-ids sg-xxxxxxxx

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type c4.2xlarge --key-name your-key —security-group-ids sg-xxxxxxxx

How to Associate an Elastic IP Address to an Instance

Requires an instance id and Elastic IP address.

aws ec2 associate-address --instance-id i-xxxxxxxxxxxxxxxx --public-ip xxx.xxx.xxx.xxx

aws ec2 associate-address --instance-id i-xxxxxxxxxxxxxxxx --public-ip xxx.xxx.xxx.xxx

How to Terminate an Instance

Requires an instance id(s).

aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxx

aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxx

Filed Under: Cloud Tagged With: aws, cli, ec2

JQ Proccessor

May 19, 2017

The AWS CLI spits out a JSON output after each successful execution. If you need to grab the result, assign it to a variable, and use it for your subsequent scripts, you need some kind of JSON parser. You can use a tool like jq which will process or filter out the result for you. From jq’s website,

jq is a tool for processing JSON inputs, applying the given filter to its JSON text inputs and producing the filter’s results as JSON on standard output. The simplest filter is ., which is the identity filter, copying jq’s input to its output unmodified (except for formatting).

In this example, we will use the AWS CLI to give us a list of running EC2 instances. We will then dump the output into a file called output.json. We will then filter out the “InstanceId” by running it through cat and the jq processor. We will then assign the result to a variable called INSTANCE, and then finally use that variable to associate our instance to an Elastic IP Address.

aws ec2 describe-instances --filters Name=instance-state-name,Values=running > output.json
INSTANCEID=$(cat output.json | jq '.Reservations[].Instances[] | {InstanceId} | .InstanceId' --raw-output)
aws ec2 associate-address --instance-id $INSTANCEID --public-ip xxx.xxx.xxx.xxx

aws ec2 describe-instances --filters Name=instance-state-name,Values=running > output.json INSTANCEID=$(cat output.json | jq '.Reservations[].Instances[] | {InstanceId} | .InstanceId' --raw-output) aws ec2 associate-address --instance-id $INSTANCEID --public-ip xxx.xxx.xxx.xxx

Filtering a nested JSON can be a bit tricky. In this particular case, we are using a filter you’ll find inside the single quote right after the jq command. To remove quotes from our result, I’m using –raw-output switch. Finally, I then associate our instance to an elastic public IP address.

jq is a very handy tool.

Filed Under: Cloud, Linux Tagged With: aws cli, jq

Install Boto3 AWS SDK for Python

May 18, 2017

Boto3 is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of Amazon services like S3 and EC2. Boto provides an easy to use, object-oriented API as well as low-level direct service access.

To install on Mac

sudo easy_install pip
sudo pip install —ignore-installed six boto3

sudo easy_install pip sudo pip install —ignore-installed six boto3

You need to set up your credentials and config file to authenticate to AWS first. The files are:

.aws/credentials
.aws/config

After it’s installed and configured, try using Boto3 to fetch your AWS S3 buckets.

import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
  print(bucket.name)

import boto3 s3 = boto3.resource('s3') for bucket in s3.buckets.all(): print(bucket.name)

Your S3 buckets will be listed after execution.

Boto3 has access to other AWS resources like EC2, meaning you can start and stop instances via Python and Boto3.

Filed Under: Cloud, Linux Tagged With: aws, boto2, python, sdk

Multisite to Single Site

May 10, 2017

I have a couple of WordPress blogs that are running multisite. I decided I don’t need the other blogs. So, I converted both sites to the standard or single WordPress site. The good news is that there’s no need to reinstall WordPress. It requires just a simple edit of a couple of WordPress files. The conversion was not as bad as I initially thought. Just follow the steps below.

5 Easy Steps

  1. Back up your database — just in case.
  2. Delete the blogs you don’t want.
  3. Edit wp-config.php.
  4. Edit .htaccess.
  5. Clean up your database.

1. Backing up your database — just in case. You can use several tools for backing up your blogs. You can use PHPMyAdmin or a WordPress backup plugin. You can also use the WordPress Export feature to save the blog in XML format in case you want to restore it in the future.

2. Delete the blogs you don’t want – Go to All blogs in the WordPress Dashboard. If you have multiple blogs, it will be listed here. Delete the ones you don’t want.

3. Edit wp-config.php. Remove the Multisite entries in wp-config.php.

define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
$base = '/wordpress/';
define( 'DOMAIN_CURRENT_SITE', 'localhost' );
define( 'PATH_CURRENT_SITE', '/wordpress/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

define( 'MULTISITE', true ); define( 'SUBDOMAIN_INSTALL', false ); $base = '/wordpress/'; define( 'DOMAIN_CURRENT_SITE', 'localhost' ); define( 'PATH_CURRENT_SITE', '/wordpress/' ); define( 'SITE_ID_CURRENT_SITE', 1 ); define( 'BLOG_ID_CURRENT_SITE', 1 );

4. Change your .htaccess to the standard WordPress configuration.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress

5. Finally, remove the database tables that are no longer needed.

wp_blogs
wp_blog_versions
wp_registration_log
wp_signups
wp_site
wp_sitemeta

wp_blogs wp_blog_versions wp_registration_log wp_signups wp_site wp_sitemeta

Filed Under: WP Tagged With: convert, multisite, single

Starting and Stopping Services

May 10, 2017

Linux is quite stable, but there are times that you may have to restart an application or a service after a configuration change. For the change to take effect, a restart of a service is required. The good news is starting and stopping services in Linux are quite easy.

The old way.

# Starting and stopping MySQL
$ sudo /etc/init.d/mysql start
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mysql restart
# Starting and stopping Apache
$ sudo /etc/init.d/apache2 start
$ sudo /etc/init.d/apache2 stop
$ sudo /etc/init.d/apache2 restart
# Starting and stopping the network
$ sudo /etc/init.d/networking start
$ sudo /etc/init.d/networking stop
$ sudo /etc/init.d/networking restart

# Starting and stopping MySQL $ sudo /etc/init.d/mysql start $ sudo /etc/init.d/mysql stop $ sudo /etc/init.d/mysql restart # Starting and stopping Apache $ sudo /etc/init.d/apache2 start $ sudo /etc/init.d/apache2 stop $ sudo /etc/init.d/apache2 restart # Starting and stopping the network $ sudo /etc/init.d/networking start $ sudo /etc/init.d/networking stop $ sudo /etc/init.d/networking restart

Via service for Debian|Ubuntu distros

# Starting and stopping MySQL
$ sudo service mysql start
$ sudo service mysql stop
$ sudo service mysql restart
# Starting and stopping Apache
$ sudo service apache2 start
$ sudo service apache2 stop
$ sudo service apache2 restart
# Starting and stopping the network
$ sudo service networking start
$ sudo service networking stop
$ sudo service networking restart

# Starting and stopping MySQL $ sudo service mysql start $ sudo service mysql stop $ sudo service mysql restart # Starting and stopping Apache $ sudo service apache2 start $ sudo service apache2 stop $ sudo service apache2 restart # Starting and stopping the network $ sudo service networking start $ sudo service networking stop $ sudo service networking restart

Via Systemctl for CentOS|Redhat distros

# Starting and stopping MySQL
$ sudo systemctl start mysqld
$ sudo systemctl stop mysqld
$ sudo systemctl restart mysqld
# Starting and stopping Apache
$ sudo systemctl start httpd
$ sudo systemctl stop httpd
$ sudo systemctl restart httpd
# Starting and stopping the network
$ sudo systemctl start network
$ sudo systemctl stop network
$ sudo systemctl restart network

# Starting and stopping MySQL $ sudo systemctl start mysqld $ sudo systemctl stop mysqld $ sudo systemctl restart mysqld # Starting and stopping Apache $ sudo systemctl start httpd $ sudo systemctl stop httpd $ sudo systemctl restart httpd # Starting and stopping the network $ sudo systemctl start network $ sudo systemctl stop network $ sudo systemctl restart network

Filed Under: Linux Tagged With: services, start, stop

Comparing Time

May 9, 2017

Here’s a neat little PHP script that compares a set date/time to the current time. The time() function sets the $now variable to the current Unix timestamp. The strtotime() function converts a date string to a Unix timestamp. The result is assigned to the $setdate variable. The two variables containing Unix timestamps are then compared. A simple if-then statement determines if the date is in the past or future.

date_default_timezone_set('America/Los_Angeles');
$setdate = strtotime("May 9, 2017 12:27PM");
$now = time();
if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

date_default_timezone_set('America/Los_Angeles'); $setdate = strtotime("May 9, 2017 12:27PM"); $now = time(); if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

You may have to change the default timezone if your server is in a different timezone.

Filed Under: PHP Tagged With: comparison, date, time, unix timestamp

Resize Window To Any Resolution

May 9, 2017

If you record videos of your desktop, especially the Terminal, it’s a challenge to get your window to fit properly in a 16:9 format. Without the proper tool, eyeballing a window to fit a certain resolution is a difficult task to do.

One of the tools I use to set my window to a resolution that I want is to use my browser as a guide. There are several websites out there that allow you to set your browser to any size or resolution. Head over to resizemybrowser.com.  They have several presets in varying resolutions.

If you don’t like any of their presets, then you can create your own. In addition, you can also manually adjust your current window to any size you want. The website gives you feedback of your resolution in real time. Once you set your browser to the right size, then you can match your Terminal with the size of your browser.

Filed Under: Mac Tagged With: browser, resolution

  • Home
  • About
  • Archives

Copyright © 2023