This post will cover installing oracle xe on CentOS 6.4 using vagrant and ansible. If you have not already, read the first post on creating a vagrant centos base box
Ansible is an IT orchestration engine/configuration management system, that lets you easily describe how you would like your servers to look and then automate it. It differs from Chef and Puppet, as it requires nothing to be installed on the server, it uses ssh. Tasks are described in yml, which can be written and read even by non programmers.
Vagrant uses a Vagrantfile to describe the type of machine you would like to build, and the file should be stored in the root directory of your source code. Next we create the Vagrantfile
1234567
$ mkdir ../vagrant-centos-oracle
$ cd ../vagrant-centos-oracle
$ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on
`vagrantup.com`for more information on using Vagrant.
Edit the Vagrantfile and change config.vm.box to:
1
config.vm.box="centos-64-x86_64"
Now we run:
12345678910111213141516171819
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos-64-x86_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22=> 2222 (adapter 1)[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Mounting shared folders...
[default] -- /vagrant
$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$
Great it works, we have a basic CentOS 6.4 Minimal install working, this is exactly like the box we created with veewee. Not too interesting just yet. We can add other customizations, like increase the memory size by adding this to the Vagrantfile
You will also notice, that /vagrant folder is a shared folder with your host machine, the root of the directory where the Vagrantfile is stored.
12345678910
[vagrant@localhost ~]$ ls /vagrant/
Vagrantfile
[vagrant@localhost ~]$ touch /vagrant/abd.txt
[vagrant@localhost ~]$ ls /vagrant/
abd.txt Vagrantfile
[vagrant@localhost ~]$ exitlogoutConnection to 127.0.0.1 closed.
$ ls
Vagrantfile abd.txt
Creating the playbook
Ansible has playbooks, which basically describe all the steps ansible needs to execute to get your system to the required state. All the steps are described in a yml file, with specific keywords for each task. You can read more at: http://www.ansibleworks.com/docs/playbooks.html
Before we start with our oracle playbook, we need to create a few directories.
We need to tell Vagrant to use the ansible playbook, by adding the following to the Vagrantfile. We also set the ansible verbose setting to extra, so we can see what is going on.
We can now test that oracle works from inside the VM:
12345678910111213
$ vagrant up
$ vagrant ssh
Last login: Sun Nov 10 11:35:22 2013 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ sqlplus system/manager@localhost
SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 10 12:37:23 2013
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Vagrant port fowarding – connect from your host
If we try an connect from the host to the VM, it wont work. To get this to work we will use vagrant port forwarding by adding the following to our Vagrantfile.
We also need to modify our playbook, to disable iptables and to configure oracle to accept remote connections, from outside the VM.
oracle-xe.yml
123456
-name:stop ip tablesshell:service iptables stop-name:set oracle listenersudo:Falseshell:ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe /u01/app/oracle/product/11.2.0/xe/bin/sqlplussystem/manager@localhost < /vagrant/oracle/set_listener.sql
Also create a new file: oracle/set_listener.sql
set_listener.sql
123
EXECDBMS_XDB.SETLISTENERLOCALACCESS(FALSE);quit;/
Lets see if we can connect from our host to the VM:
12345678910111213
$ vagrant provision
$ sqlplus system/manager@localhost
SQL*Plus: Release 11.2.0.3.0 Production on Sun Nov 10 15:19:50 2013
Copyright (c) 1982, 2012, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> exit$ curl -v http://localhost:8080/apex/f?p=4950:1
Great, it works. This may seem like an awful amount of work just to setup a centos vm with oracle, though remember, Once you have done this once, no one else needs to follow the same steps again. All they need to do is issue a vagrant up from your source code repo.
This post will discuss building a CentOs 6.4 vagrant box, and in later posts we will use this box and ansible to further define our machines, such as installing oracle xe.
About Vagrant
Vagrant lets you create and configure lightweight, reproducible, and portable development environments. All too often you will find teams developing off a central server, purely because of the pain of setting up a dev environment. Nothing is automated. There are many challenges with this approach. IMHO this slows down a team, and experimentation is discouraged and you end up “stepping on each others code”.
Vagrant can solve some these challenges, ever started a new project and spent a few days just getting your environment setup? Well with vagrant this should be a thing of the past.
Before you start
You will need some baseline software before we begin. Make sure you have the following installed.
$ git version
git version 1.8.3.4 (Apple Git-47)$ vagrant --version
Vagrant 1.3.5
$ VBoxManage --version
4.3.2r90405
$ veewee version
Version : 0.3.12 - use at your own risk
Build a CentOS 6.4 base box
You can find a number of vagrant base boxes online, but i prefer creating one from scratch. We can do that easily with veewee.
First we clone the repo: git clone https://github.com/ismaild/vagrant-boxes.git. I have disabled chef and puppet since we will be using ansible. Feel free to add it back in if you want it, which you can by editing: definitions/CentOS-6.4-x86_64/definition.rb
Veewee will automatically try and download the required ISO files. If you already have them, you can put them in the iso directory and it will not need to download.
Just make sure the version of the virtualbox guest additions matches your installed version of virtualbox.
Then run the following commands:
12345678910
$ cd vagrant-boxes
$ veewee vbox list
# if no boxes defined$ veewee vbox define CentOS-6.4-x86_64 CentOS-6.4-x86_64-minimal
# Build the box$ veewee vbox build CentOS-6.4-x86_64
# Eject the disks from the running VM and shutdown.# Package the box$ vagrant package --base CentOS-6.4-x86_64 --output CentOS-6.4-x86_64.box
$ vagrant box add centos-64-x86_64 CentOS-6.4-x86_64.box
Recently i have had to get my dev environment setup to connect to oracle for a project.
Getting the client setup can sometimes be a real pain with a Mac. Oracle does provide client libraries but they are painfully slow in updating them and fixing bugs. There was an outstanding segmentation fault bug for over 2 years.
Here are the steps to get it setup on Mac, and it would probably work for ubuntu etc, just have not tested it.
Which will add the environment variables to your .bash_profile, You can also find this in my dotfiles
Test Sql*Plus works
123456789101112
sqlplus user/pass@orademo
SQL*Plus: Release 11.2.0.3.0 Production on Thu Sep 12 09:19:55 2013
Copyright (c) 1982, 2012, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select table_name from user_tables;
You could also store multiple versions of the client, with different .oracle_client files, with a small shell script you could switch between different versions (i.e like when oracle does not fix a bug for 2 years! )
Install Ruby oci gem
123456
cd /usr/local/oracle/product/instantclient_64/11.2.0.3.0/lib
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib
# Make sure sdk directory is in /usr/local/oracle/product/instantclient_64/11.2.0.3.0/libgem install ruby-oci8
Test that it works…
12345678
irbirb(main):001:0>require'oci8'irb(main):006:0>o=OCI8.new('user','pass','127.0.0.1/orademo')=>#<OCI8:user>irb(main):011:0>o.exec('select * from dual')do|r|putsr.join(',');endX=>1irb(main):012:0>exit
If you have issues connecting with an SID or Service Name, try using the IP.
You now have a working oracle database client, and can connect to it from ruby.
CMD (the default execution command for the container)
EXPOSE (set the port to be publicly exposed)
ENV (set environment variables)
ADD (add files from source and copy them to the container)
ENTRYPOINT (configure the container to run as an executable)
VOLUME (add a volume)
USER (set the user)
WORKDIR (set working directory)
MongoDB dockerfile
We create a dockerfile, and just use all the same commands we used previously touch Dockerfile
123456789101112131415
FROM ubuntu:latest
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
RUN apt-get update
RUN apt-get install mongodb-10gen
RUN mkdir -p /data/db
EXPOSE 27017
CMD ["usr/bin/mongod", "--smallfiles"]
Then we issue:
sudo docker build -t codiez/mongodb .
and start it up with…
sudo docker run -d codiez/mongodb
In the next post, we will discuss creating a SAAS database service using docker and mongodb.
MongoDB starts succesfully in the container. Now we need to commit the container, to save the state and push to the docker index.
sudo docker commit a1f0680f8458 codiez/mongodb
Remember to replace your container id, mine was a1f0680f8458 which you can see at the prompt, and your <username>/<container_name>
12345678910111213141516
dock@saas:~$ sudo docker commit a1f0680f8458 codiez/mongodb
dock@saas:~$ sudo docker login
Username: codiez
Password:
Email: ismail@codiez.co.za
Login Succeeded
dock@saas:~$ sudo docker push codiez/mongodb
The push refers to a repository [codiez/mongodb](len: 1)Processing checksums
Sending image list
Pushing repository codiez/mongodb (1 tags)Pushing 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c
Image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c already pushed, skipping
Pushing tags for rev [8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c] on {https://registry-1.docker.io/v1/repositories/codiez/mongodb/tags/latest}Pushing f0ab8043e4e8135379d35410a4847769efb9245d8d4817cb24a2196c434c8506
Once that completes, we can start MongoDB in the container, and map it to a port by running:
sudo docker run -d -p 27017 codiez/mongodb /usr/bin/mongod --smallfiles
This basically runs the container, based on the codiez/mongodb image, with the command /usr/bin/mongod and maps the default mongodb port 27017 to an external port.
Check that the container is running and get the port:
123
dock@saas:~$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
a1f0680f8458 codiez/mongodb:latest /usr/bin/mongod 5 seconds ago Up 4 seconds 49157->27017
You can also inspect the image to grab the port by running sudo docker inspect container_id
Now, we can test an external connection to MongoDB, test from your local machine and connect to the VM IP with the port for your container:
123456789
$ mongo 192.168.0.21:49157
MongoDB shell version: 2.4.5
connecting to: 192.168.0.21:49157/test
> db
test> db.posts.insert({title:"Hello MongoDB in Docker"})> db.posts.find(){"_id" : ObjectId("5227058d112c68baaa3b94d9"), "title" : "Hello MongoDB in Docker"}>
Update: Stop the container and restart it since we do not want to commit with the test data.
One final commit to save the command and port mapping. Also notice you do not have to enter in the entire container id when running commands, just the first few characters.
1234
dock@saas:~$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
298a43e2f98e codiez/mongodb:latest /usr/bin/mongod --sm 35 seconds ago Up 34 seconds 49164->27017
dock@saas:~$ sudo docker commit -run '{"Cmd": ["/usr/bin/mongod", "--smallfiles"], "PortSpecs": [":27017"]}' 298a4 codiez/mongodb
Now that we have an image, we can run docker pull codiez/mongodb to grab the container and run it with docker run -d codiez/mongodb
I have been playing around recently with Docker. It could really simplify deployments, creating SAAS services or even creating your own personal PAAS.
What is docker?
Docker is an open-source project to easily create lightweight, portable, self-sufficient containers from any application. The same container that a developer builds and tests on a laptop can run at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more.
Containers vs Virtual Machines
Virtual Machines: require a complete operating system image, with allocated resources to run. They take a long time to bootup, and have quite a bit of overhead.
Containers: are much more lightweight, since there is no overhead of a complete virtual environment, with the kernel managing the memory and access to the file system. This also means you can bootup an application in seconds.
Docker works best with the 3.8 kernel due to a bug in lxc which can cause some issues if you are on 3.2. run uname -r to check which version you are on, and run the following if you are not on 3.8
I have recently have to set up my dev environment once again, which can be quite a bit of a pain, especially if you do not have something like boxen.
This time however i decided to avoid macports all together, one of the “benefits” of macports over homebrew is the ability to run different versions since macports will install into /opt , though its a feature i never used, and i have had a bunch of strange build incompatabilities that has taken me quite a bit of time to debug.
Here are the steps
Uninstall macports
1
sudo port -fp uninstall --follow-dependents installed
Though when working with MongoDB arrays in rails and forms to edit the data, you could end up doing a bunch of controller logic and/or js logic just to get the data in.
This is especially true if you want to just store a bunch of strings in the array. I tried many different ways of doing this, but the simplest solution with out any changes to your controller or forms is to just use a virtual attribute on your model.
Any comma seperated values entered will be stored correctly in MongoDB, you could also use this to apply any transformations to the data before it saved, i.e captilizing each value.
We ended up using this bit of code quite often, so i extracted it out to lib, and then packaged it into a gem.