вторник, 22 октября 2013 г.

How to use Amazon Web Services Auto Scaling Groups

This post we will cover all the commands to create and managed Amazons Auto Scaling Groups

What is an Auto Scaling Group?

Auto Scaling allows you to scale your Amazon EC2 capacity up or down automatically according to conditions you define. With Auto Scaling, you can ensure that the number of Amazon EC2 instances you’re using increases seamlessly during demand spikes to maintain performance, and decreases automatically during demand lulls to minimize costs.
Now you will need an Amazon Machine Image (AMI)

What is an Amazon Machine Image (AMI)?

An Amazon Machine Image (AMI) is a special type of pre-configured operating system and virtual application software which is used to create a virtual machine within the Amazon Elastic Compute Cloud (EC2). It serves as the basic unit of deployment for services delivered using EC2.
Let’s imagine you have an AMI: ami-xxxxxx
Before you can create an Auto Scaling Group you need a Launch Configuration

What is a Launch Configuration?

A Launch Configuration defines the AMI to be used, the size of the EC2 instances to be used, the security group the instances will live in and the private key used to decrypt the password in Windows or for gaining access via SSH
How to create a Launch Configuration?
?
1
2
// create launch config
as-create-launch-config my-launch-config-1 --image-id ami-xxxxxx --region eu-west-1 --instance-type m1.small --group my-securitygroup-sg --key myKey
How to check the launch configuration has been created?
?
1
2
// check launch config has been created
as-describe-launch-configs --headers --region eu-west-1  --max-records 50
or check just your launch config has been create
?
1
2
// check your launch config has been created
as-describe-launch-configs my-launch-config-1 --headers --region eu-west-1  --max-records 50
How to delete a launch configuration?
?
1
2
// delete launch config
as-delete-launch-config my-launch-config-1 --region eu-west-1
How to creating an Auto Scaling Group?
?
1
2
// create auto scaling group
as-create-auto-scaling-group my-auto-scaling-group --region eu-west-1 --launch-configuration my-launch-config-1 --availability-zones eu-west-1a eu-west-1b eu-west-1c --min-size 3 --max-size 3 --desired-capacity 3 --default-cooldown 5 --grace-period 5 --tag "k=Name, v=my-server, p=true" --tag "k=enabled, v=true, p=true"
How to check the Auto Scaling Group has been created?
?
1
2
// check auto scaling group has been created
as-describe-auto-scaling-groups --headers --region eu-west-1
How to add or update the Auto Scaling Group tags?
Add or update the Name tag
?
1
2
// add or update the tag
as-create-or-update-tags --tag "id=my-auto-scaling-group, t=auto-scaling-group, k=Name, v=my-server, p=true" --region eu-west-1
Add or update a custom ROLE tag
?
1
2
// add or update the tag
as-create-or-update-tags --tag "id=my-auto-scaling-group, t=auto-scaling-group, k=ROLE, v=production, p=true" --region eu-west-1
Add or update a custom ALLOWED_IPS tag
?
1
2
// add or update tag
as-create-or-update-tags --tag "id=my-auto-scaling-group, t=auto-scaling-group, k=ALLOWED_IPS, v=XXX.XXX.XXX.XXX, p=true" --region eu-west-1
How to scale up the Auto Scaling Group?
This would scale your group of instances up to 6 and set a max of 12
?
1
2
// scale up
as-update-auto-scaling-group my-auto-scaling-group --region eu-west-1 --min-size 6 --max-size 12 --desired-capacity 6
How to scale down the Auto Scaling Group?
This would scale your group of instances up to 3 and set a max of 6
?
1
2
// scale down
as-update-auto-scaling-group my-auto-scaling-group --region eu-west-1 --min-size 3 --max-size 6 --desired-capacity 3
How to set zero instances in the Auto Scaling Group?
This would remove all instances, but keep the auto scaling group configuration ready for use in the future
?
1
2
// with zero instances
as-update-auto-scaling-group my-auto-scaling-group --desired-capacity 0 --max-size 0 --min-size 0 --region eu-west-1
How to delete an Auto Scaling Group?
?
1
2
// delete auto scaling group
as-delete-auto-scaling-group my-auto-scaling-group --region eu-west-1
How to get the Auto Scaling Groups latest activities?
This is handy for trouble shooting problems with your Auto Scaling Group
?
1
2
// to get latest actions
as-describe-scaling-activities --auto-scaling-group my-auto-scaling-group --region eu-west-1
How to update the Auto Scaling Group Configuration
This command lets you change auto scaling group configuration, zones, the min, max and desired number of instances
?
1
2
// with many options
as-update-auto-scaling-group my-auto-scaling-group --launch-configuration my-launch-config-2 --availability-zones eu-west-1a eu-west-1b eu-west-1c --desired-capacity 3 --max-size 3 --min-size 3 --region eu-west-1
How to change the Auto Scaling Group’s Launch Configuration
This will set a new launch configuration for the auto scaling group
?
1
2
// with new launch config
as-update-auto-scaling-group my-auto-scaling-group --launch-configuration my-launch-config-2 --region eu-west-1

Further Reading

пятница, 18 октября 2013 г.

Number Formatting in JavaScript

I got to this question page hoping to find how to format numbers in JavaScript, without introducing yet another library. Here's what I've found:

Rounding floating-point numbers

The equivalent of sprintf("%.2f", num) in JavaScript seems to be num.toFixed(2), which formats num to 2 decimal places, with rounding (but see @ars265's comment about Math.roundbelow).
12.345.toFixed(2); // returns "12.35" (rounding!)
12.3.toFixed(2); // returns "12.30" (zero padding)

Exponential form

The equivalent of sprintf("%.2e", num) is num.toExponential(2).
33333 .toExponential(2); // "3.33e+4"
//   ^ Note the space, which keeps the . from being a decimal point.
//     Leaving out the space is a syntax error :-(

Hexadecimal and other bases

To print numbers in base B, try num.toString(B). JavaScript supports automatic conversion to and from bases 2 through 36 (in addition, some browsers have limited support for base64 encoding).
3735928559 .toString(16); // to base 16: "deadbeef"
parseInt("deadbeef", 16); // from base 16: 3735928559

Reference Pages

Mozilla reference page for toFixed() (with links to toPrecision(), toExponential(), toLocaleString(), ...)

воскресенье, 6 октября 2013 г.

Structuring an Angular Project

ngappWe can build amazing SPA and HTML5 applications when choosing a powerful framework like Angular, Durandal, Ember, or Backbone. And while I love my personal favorites Angular and Durandal, it’s not all magic. There is a learning curve and some things are helpful to decide up front. Once you choose your presentation framework, you should decide how you will organize the structure of your project.
If you are interested in SPA, HTML5, Angular, BreezeJS or JavaScript patterns then you will love my upcoming course at Pluralsight, due out in September 2013. Or if you prefer Knockout and Durandal check out my courses on Pluralsight today.
In this post I’ll show one way how I organize my Angular based projects and discuss why I like it.
Before you ask “Why didn’t he mentionKnockout.js? Doesn’t he love it anymore?”, let me answer that up front. Knockout.js is awesome. I love it. I use it. In fact, it is an integral piece of Durandal such that I often refer to Durandal as Durandal/Knockout. But that is the point: Durandal and Angular are frameworks. Frameworks handle a lot of things for you that cover most of the plumbing you need in a SPA. Knockout handles 1 thing: data binding. Now, it handles it very well and has some awesome features that crop out of it, like custom binding handlers and extensions. But I do not consider Knockout a complete framework, nor do I believe it is intended to be one. So you’ll often hear me talk about Durandal and Knockout together as a framework choice.

Sorting Boxes

Most of my client code ends up in one of these 4 folders. I like to keep code I wrote separate from code someone else wrote. Sounds pretty simple when you say that but I see a lot of projects where everything gets tossed in the same folder and it makes a mess to find what you need or run code analysis against just your code. (Who wants want to run static code analysis against angular or knockout?) I also want my tests separated from my app and all of my assets (images, fonts, css) in yet another folder.
/app
/content
/scripts
/test
So for me, all of my code goes in the app folder, 3rd party or vendor scripts go in thescripts folder, assets in the content folder, and tests in the test folder.

App Code

My app folder is where all of my files I wrote for my app reside. This includes JavaScript and HTML files. The root of the folder is where I put my app.js which is my boot file. This is where all the magic starts and the application gets kicked off. For angular this means the modules get loaded and run. I generally name my root module app. I also usually create a config.js here to store all of my commonly used variables across the app. For angular I store these inapp.value or app.constant.
/app
    /controllers/
    /services/
    /views/
    /app.js
    /config.js
/content
/scripts
/test

Controllers

The controllers folder is where all of my controllers live. Makes sense, an app usually has several of them so why not put them all in one place. Notice what is not there though? The word “controller” in the name of the files. I’m not a fan of naming just for the sake of it. By putting all of my controllers in a controllers folder, guess what, I know they are controllers. I do not find an advantage in naming them “controller.session.js” or “sessionController.js”. If you are wondering about the casing, I use camelCase because I it feels right :) . That’s just how I roll. When I use a controller, Angular creates a new one for each view instance. Should it be PascalCase … that’s your choice. It’s more important to be consistent and understand that controllers come and go, and you can test this by tapping into the $destroy event.

Services

The services folder contains all of my services for the SPA. When I say services here I mean a unit of code that provides a discrete service to the rest of the app. I am not specifically talking about the Angular provider, service or factory, though these services may be constructed from those Angular features. I will generally have services for typical uses such as a logger, datacontext, routing, or storage. Sometimes I will add more specialized services here too such as work in progress or client side repositories. My services follow these 3 criteria: any set of code that can be …
  • reused
  • tested on its own
  • encapsulated
I also put all of my directives in the services folder.

Directives

Generally I put them in a single file named directives.js. Some folks like to separate their directives, and I get it … I just don’t agree. I will often have several of these, but they are all small and often I want to look at other directives when I am developing new ones (for ideas). Now, I will create multiple files for directives if there is an obvious logical separation. For example, if I create a bunch of directives that apply just to using BreezeJS functionality, then sure, I’ll make a file just for those. And what goes in a directive for me? Ideal candidates are any DOM interaction or events that are not covered by out of the box Angular directives. I also use this to wrap 3rd party widgets that I like. If I had a ton of directives and several files, then I would consider a new folder for them too. When I use filters, I also create a filters file in this folder. Some projects I don’t use filters. Why? Well, that’s a topic for another post.

Views

The final core folder is views, which is where my HTML resides. This includes inclusive views such as sessions.html. But it also includes partial views that may be inclusive or only intended to be used in another view. For example, some partial views have their own Angular controllers. These views can be placed in other views, and the controller comes with them. So they work anywhere. Then there are partial views that rely on some parent view. I tend to try to stick with the latter (views that have their own controller) … that’s just my preference. But I don’t go nuts either way.

Your Choice

Ultimately how you organize your code is entirely up to you (and your team). I find it is immensely valuable to think it through before I start working with a team and tossing files all over the place. Having logical sorting boxes for all of your code just makes life easier. There are many ways to skin this cat so don’t fret if you don’t follow this exact pattern. That’s what it is: a pattern. Not gospel.

Next

For smaller apps I find this structure quite simple, organizing by types. Once the app grows and starts expanding into multiple features and especially modules, I like to change the structure to group by feature. I’ll explore this side by side a bit more in a future post. Both structures have value and really depend on how easy it is to find what you are looking for.

Update

In the comments Joel mentions a great post by Cliff Meyers that discusses another approach to organizing an Angular app. Cliff’s approach is to organize by functional area. There is much wisdom in this choice too. Where I differ personally is if the app is in a single module I consider the module the functional area. Then once within that area, I choose to separate by type as discussed above. For me, once I see a different functional features, I consider making a new Angular module and separating from there. But again, these depend on your comfort. Happy coding!

понедельник, 30 сентября 2013 г.

Test remote SQL connectivity EASILY!

I learned about something very useful today and thought I’d share.
Have you ever been troubleshooting a problem with remote SQL Iproxy MP, remote DB, etc) and wanted to test to see if the local system account (or any account for that matter) could make a remote connection to SQL but you didn’t want to install the SQL tools just to make that test?  Seems there is a file type – UDL file – that you can simply create that will bring up a window to allow testing of remote connections to SQL.
Just go anywhere on your system and create an empty text file named anything but instead of txt make sure the extension is UDL.  Then, double-click on the file and up pops a SQL connectivity window to allow testing of remote SQL connections.
clip_image002
Very easy to use to test connectivity with a known user account or as local system (using PSExec).

четверг, 19 сентября 2013 г.

Windows 7 — не запускаются программы. Ошибка 0xc0000005

Если с вами случилась такая же беда, то знайте — это связано с обновлениемKB2859537.
Чтобы его удалить, запустите cmd.exe от имени администратора и наберите в консоли:
wusa.exe /uninstall /kb:2859537
После перезагрузки компьютера, все придет в норму. Рекомендую отключить Автоматическое обновление Windows, чтобы больше не сталкиваться с этой проблемой.
В противном случае вам придется еще удалить обновление  KB2872339, которое вышло после KB2859537 и вызывает такую же ошибку.
Чтобы его удалить, запустите cmd.exe от имени администратора и наберите в консоли:
wusa.exe /uninstall /kb:2872339