12 January, 2013
Amazon Server blog
I continue to write articles about Amazon Services and servers here: Amazon Server
07 January, 2013
Convert image to base64
It is extremely simple, and without server-side:
You may use it at: ImageToBase64 or directly from here:
See this project on GitHub
<script>
function convertToBase64(event) {
var file = event.target.files[0];
var reader = new FileReader();
reader.onload = function (event) {
document.getElementById('text').value = event.target.result;
document.getElementById('text').select();
};
reader.readAsDataURL(file);
}
</script>
<input id="file" type="file" onchange="convertToBase64(event)">
<textarea id="text"> </textarea>
You may use it at: ImageToBase64 or directly from here:
See this project on GitHub
06 January, 2013
Merge log files from multiple servers
If you have some similar servers (e.g. balanced EC2-instances) and need to analyze log files from them - it is possible to automatize downloading and merging all these files via shell script like this:
#!/bin/bash
LOG_DIR=log-`date +%Y.%m.%d_%H.%M.%S`
mkdir -p $LOG_DIR
scp -C ec2-user@node1.server.com:/log/file.log $LOG_DIR/file1.log
scp -C ec2-user@node2.server.com:/log/file.log $LOG_DIR/file2.log
scp -C ec2-user@node3.server.com:/log/file.log $LOG_DIR/file3.log
cat $LOG_DIR/*.log | sort >$LOG_DIR/files.log
#!/bin/bash
LOG_DIR=log-`date +%Y.%m.%d_%H.%M.%S`
mkdir -p $LOG_DIR
scp -C ec2-user@node1.server.com:/log/file.log $LOG_DIR/file1.log
scp -C ec2-user@node2.server.com:/log/file.log $LOG_DIR/file2.log
scp -C ec2-user@node3.server.com:/log/file.log $LOG_DIR/file3.log
cat $LOG_DIR/*.log | sort >$LOG_DIR/files.log
Also it is a good idea to use ssh-agent to keep private keys.
05 January, 2013
Count string characters in Python
Simple code that calculates frequency of letters:
from collections import Counter
s = '''
She sells sea-shells on the sea-shore.
The shells she sells are sea-shells, I'm sure.
For if she sells sea-shells on the sea-shore
Then I'm sure she sells sea-shore shells.
'''
print Counter(s)
Result:
Counter({'s': 32, 'e': 29, ' ': 25, 'l': 18, 'h': 16, 'a': 7, 'r': 7, '-': 6, 'o': 6, '\n': 5, '.': 3, 'n': 3, "'": 2, 'I': 2, 'T': 2, ' m': 2, 'u': 2, 't': 2, ',': 1, 'F': 1, 'S': 1, 'f': 1, 'i': 1})
from collections import Counter
s = '''
She sells sea-shells on the sea-shore.
The shells she sells are sea-shells, I'm sure.
For if she sells sea-shells on the sea-shore
Then I'm sure she sells sea-shore shells.
'''
print Counter(s)
Result:
Counter({'s': 32, 'e': 29, ' ': 25, 'l': 18, 'h': 16, 'a': 7, 'r': 7, '-': 6, 'o': 6, '\n': 5, '.': 3, 'n': 3, "'": 2, 'I': 2, 'T': 2, ' m': 2, 'u': 2, 't': 2, ',': 1, 'F': 1, 'S': 1, 'f': 1, 'i': 1})
04 January, 2013
Recover broken Amazon EC2 instance
Sometimes shit happens, accidentally.
Especially when you lost ssh access to your virtual server (e.g. due to errors in ~/.ssh/authorized_keys, /etc/sudoers, /etc/group, /etc/shadow or another important config file).
When you catch errors like:
No supported authentication methods available
or
sudo
sudo: >>> /etc/sudoers: syntax error near line 1 <<<
sudo: parse error in /etc/sudoers near line 1
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
Don't worry, inside the Amazon cloud you can repair almost all.
Follow these simple steps to solve the problem:
Especially when you lost ssh access to your virtual server (e.g. due to errors in ~/.ssh/authorized_keys, /etc/sudoers, /etc/group, /etc/shadow or another important config file).
When you catch errors like:
No supported authentication methods available
or
sudo
sudo: >>> /etc/sudoers: syntax error near line 1 <<<
sudo: parse error in /etc/sudoers near line 1
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
Don't worry, inside the Amazon cloud you can repair almost all.
Follow these simple steps to solve the problem:
- Via AWS console:
- Launch new temporary EC2 micro-instance in the same availability zone.
- Stop broken instance.
- Detach root EBS volume from the broken instance.
- Attach this EBS to the temporary instance (as /dev/sdf).
- Via SSH console from the temporary instance:
- Mount this EBS:
sudo mount /dev/xvdf /mnt - Fix all problems on mounted file system.
- Unmount this EBS:
sudo umount /mnt - Via AWS console:
- Detach this EBS from the temporary instance.
- Attach this EBS to the broken instance (as /dev/sda1).
- Start broken instance.
- Check that broken instance now is healthy (available via ssh and everything is functioning normally).
- Then you can stop temporary instance (or terminate it)
I hope this article will make someone little bit happy)
Subscribe to:
Posts (Atom)